1 /* $NetBSD: uvm_mmap.c,v 1.131 2009/08/18 02:41:31 yamt Exp $ */
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993 The Regents of the University of California.
6 * Copyright (c) 1988 University of Utah.
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the Charles D. Cranor,
25 * Washington University, University of California, Berkeley and
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
44 * @(#)vm_mmap.c 8.5 (Berkeley) 5/19/94
45 * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
49 * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.131 2009/08/18 02:41:31 yamt Exp $");
56 #include "opt_compat_netbsd.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
63 #include <sys/filedesc.h>
64 #include <sys/resourcevar.h>
66 #include <sys/mount.h>
68 #include <sys/malloc.h>
69 #include <sys/vnode.h>
74 #include <sys/verified_exec.h>
75 #endif /* NVERIEXEC > 0 */
79 #endif /* PAX_MPROTECT */
81 #include <miscfs/specfs/specdev.h>
83 #include <sys/syscallargs.h>
86 #include <uvm/uvm_device.h>
88 #ifndef COMPAT_ZERODEV
89 #define COMPAT_ZERODEV(dev) (0)
93 range_test(vaddr_t addr
, vsize_t size
, bool ismmap
)
95 vaddr_t vm_min_address
= VM_MIN_ADDRESS
;
96 vaddr_t vm_max_address
= VM_MAXUSER_ADDRESS
;
97 vaddr_t eaddr
= addr
+ size
;
99 if (addr
< vm_min_address
)
101 if (eaddr
> vm_max_address
)
102 return ismmap
? EFBIG
: EINVAL
;
103 if (addr
> eaddr
) /* no wrapping! */
104 return ismmap
? EOVERFLOW
: EINVAL
;
109 * unimplemented VM system calls:
113 * sys_sbrk: sbrk system call.
118 sys_sbrk(struct lwp
*l
, const struct sys_sbrk_args
*uap
, register_t
*retval
)
121 syscallarg(intptr_t) incr;
128 * sys_sstk: sstk system call.
133 sys_sstk(struct lwp
*l
, const struct sys_sstk_args
*uap
, register_t
*retval
)
136 syscallarg(int) incr;
143 * sys_mincore: determine if pages are in core or not.
148 sys_mincore(struct lwp
*l
, const struct sys_mincore_args
*uap
,
152 syscallarg(void *) addr;
153 syscallarg(size_t) len;
154 syscallarg(char *) vec;
156 struct proc
*p
= l
->l_proc
;
159 struct uvm_object
*uobj
;
160 struct vm_amap
*amap
;
161 struct vm_anon
*anon
;
162 struct vm_map_entry
*entry
;
163 vaddr_t start
, end
, lim
;
168 map
= &p
->p_vmspace
->vm_map
;
170 start
= (vaddr_t
)SCARG(uap
, addr
);
171 len
= SCARG(uap
, len
);
172 vec
= SCARG(uap
, vec
);
174 if (start
& PAGE_MASK
)
176 len
= round_page(len
);
182 * Lock down vec, so our returned status isn't outdated by
183 * storing the status byte for a page.
186 npgs
= len
>> PAGE_SHIFT
;
187 error
= uvm_vslock(p
->p_vmspace
, vec
, npgs
, VM_PROT_WRITE
);
191 vm_map_lock_read(map
);
193 if (uvm_map_lookup_entry(map
, start
, &entry
) == false) {
199 entry
!= &map
->header
&& entry
->start
< end
;
200 entry
= entry
->next
) {
201 KASSERT(!UVM_ET_ISSUBMAP(entry
));
202 KASSERT(start
>= entry
->start
);
204 /* Make sure there are no holes. */
205 if (entry
->end
< end
&&
206 (entry
->next
== &map
->header
||
207 entry
->next
->start
> entry
->end
)) {
212 lim
= end
< entry
->end
? end
: entry
->end
;
215 * Special case for objects with no "real" pages. Those
216 * are always considered resident (mapped devices).
219 if (UVM_ET_ISOBJ(entry
)) {
220 KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry
->object
.uvm_obj
));
221 if (UVM_OBJ_IS_DEVICE(entry
->object
.uvm_obj
)) {
222 for (/* nothing */; start
< lim
;
223 start
+= PAGE_SIZE
, vec
++)
229 amap
= entry
->aref
.ar_amap
; /* upper layer */
230 uobj
= entry
->object
.uvm_obj
; /* lower layer */
235 mutex_enter(&uobj
->vmobjlock
);
237 for (/* nothing */; start
< lim
; start
+= PAGE_SIZE
, vec
++) {
240 /* Check the upper layer first. */
241 anon
= amap_lookup(&entry
->aref
,
242 start
- entry
->start
);
243 /* Don't need to lock anon here. */
244 if (anon
!= NULL
&& anon
->an_page
!= NULL
) {
247 * Anon has the page for this entry
254 if (uobj
!= NULL
&& pgi
== 0) {
255 /* Check the lower layer. */
256 pg
= uvm_pagelookup(uobj
,
257 entry
->offset
+ (start
- entry
->start
));
261 * Object has the page for this entry
268 (void) subyte(vec
, pgi
);
271 mutex_exit(&uobj
->vmobjlock
);
277 vm_map_unlock_read(map
);
278 uvm_vsunlock(p
->p_vmspace
, SCARG(uap
, vec
), npgs
);
283 * sys_mmap: mmap system call.
285 * => file offset and address may not be page aligned
286 * - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
287 * - if address isn't page aligned the mapping starts at trunc_page(addr)
288 * and the return value is adjusted up by the page offset.
292 sys_mmap(struct lwp
*l
, const struct sys_mmap_args
*uap
, register_t
*retval
)
295 syscallarg(void *) addr;
296 syscallarg(size_t) len;
297 syscallarg(int) prot;
298 syscallarg(int) flags;
300 syscallarg(long) pad;
301 syscallarg(off_t) pos;
303 struct proc
*p
= l
->l_proc
;
307 vsize_t size
, pageoff
;
308 vm_prot_t prot
, maxprot
;
311 struct file
*fp
= NULL
;
317 #endif /* PAX_ASLR */
320 * first, extract syscall args from the uap.
323 addr
= (vaddr_t
)SCARG(uap
, addr
);
324 size
= (vsize_t
)SCARG(uap
, len
);
325 prot
= SCARG(uap
, prot
) & VM_PROT_ALL
;
326 flags
= SCARG(uap
, flags
);
328 pos
= SCARG(uap
, pos
);
332 #endif /* PAX_ASLR */
335 * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
336 * validate the flags.
338 if (flags
& MAP_COPY
)
339 flags
= (flags
& ~MAP_COPY
) | MAP_PRIVATE
;
340 if ((flags
& (MAP_SHARED
|MAP_PRIVATE
)) == (MAP_SHARED
|MAP_PRIVATE
))
344 * align file position and save offset. adjust size.
347 pageoff
= (pos
& PAGE_MASK
);
349 size
+= pageoff
; /* add offset */
350 size
= (vsize_t
)round_page(size
); /* round up */
353 * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
355 if (flags
& MAP_FIXED
) {
357 /* ensure address and file offset are aligned properly */
359 if (addr
& PAGE_MASK
)
362 error
= range_test(addr
, size
, true);
365 } else if (addr
== 0 || !(flags
& MAP_TRYFIXED
)) {
368 * not fixed: make sure we skip over the largest
369 * possible heap for non-topdown mapping arrangements.
370 * we will refine our guess later (e.g. to account for
374 defaddr
= p
->p_emul
->e_vm_default_addr(p
,
375 (vaddr_t
)p
->p_vmspace
->vm_daddr
, size
);
378 !(p
->p_vmspace
->vm_map
.flags
& VM_MAP_TOPDOWN
))
379 addr
= MAX(addr
, defaddr
);
381 addr
= MIN(addr
, defaddr
);
385 * check for file mappings (i.e. not anonymous) and verify file.
388 if ((flags
& MAP_ANON
) == 0) {
389 if ((fp
= fd_getfile(fd
)) == NULL
)
391 if (fp
->f_type
!= DTYPE_VNODE
) {
393 return (ENODEV
); /* only mmap vnodes! */
395 vp
= fp
->f_data
; /* convert to vnode */
396 if (vp
->v_type
!= VREG
&& vp
->v_type
!= VCHR
&&
397 vp
->v_type
!= VBLK
) {
399 return (ENODEV
); /* only REG/CHR/BLK support mmap */
401 if (vp
->v_type
!= VCHR
&& pos
< 0) {
405 if (vp
->v_type
!= VCHR
&& (pos
+ size
) < pos
) {
407 return (EOVERFLOW
); /* no offset wrapping */
410 /* special case: catch SunOS style /dev/zero */
411 if (vp
->v_type
== VCHR
412 && (vp
->v_rdev
== zerodev
|| COMPAT_ZERODEV(vp
->v_rdev
))) {
420 * Old programs may not select a specific sharing type, so
421 * default to an appropriate one.
423 * XXX: how does MAP_ANON fit in the picture?
425 if ((flags
& (MAP_SHARED
|MAP_PRIVATE
)) == 0) {
427 printf("WARNING: defaulted mmap() share type to "
428 "%s (pid %d command %s)\n", vp
->v_type
== VCHR
?
429 "MAP_SHARED" : "MAP_PRIVATE", p
->p_pid
,
432 if (vp
->v_type
== VCHR
)
433 flags
|= MAP_SHARED
; /* for a device */
435 flags
|= MAP_PRIVATE
; /* for a file */
439 * MAP_PRIVATE device mappings don't make sense (and aren't
440 * supported anyway). However, some programs rely on this,
441 * so just change it to MAP_SHARED.
443 if (vp
->v_type
== VCHR
&& (flags
& MAP_PRIVATE
) != 0) {
444 flags
= (flags
& ~MAP_PRIVATE
) | MAP_SHARED
;
448 * now check protection
451 maxprot
= VM_PROT_EXECUTE
;
453 /* check read access */
454 if (fp
->f_flag
& FREAD
)
455 maxprot
|= VM_PROT_READ
;
456 else if (prot
& PROT_READ
) {
461 /* check write access, shared case first */
462 if (flags
& MAP_SHARED
) {
464 * if the file is writable, only add PROT_WRITE to
465 * maxprot if the file is not immutable, append-only.
466 * otherwise, if we have asked for PROT_WRITE, return
469 if (fp
->f_flag
& FWRITE
) {
471 VOP_GETATTR(vp
, &va
, l
->l_cred
))) {
476 (SF_SNAPSHOT
|IMMUTABLE
|APPEND
)) == 0)
477 maxprot
|= VM_PROT_WRITE
;
478 else if (prot
& PROT_WRITE
) {
483 else if (prot
& PROT_WRITE
) {
488 /* MAP_PRIVATE mappings can always write to */
489 maxprot
|= VM_PROT_WRITE
;
493 } else { /* MAP_ANON case */
495 * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
500 is_anon
: /* label for SunOS style /dev/zero */
502 maxprot
= VM_PROT_ALL
;
507 if (handle
!= NULL
) {
509 * Check if the file can be executed indirectly.
511 * XXX: This gives false warnings about "Incorrect access type"
512 * XXX: if the mapping is not executable. Harmless, but will be
513 * XXX: fixed as part of other changes.
515 if (veriexec_verify(l
, handle
, "(mmap)", VERIEXEC_INDIRECT
,
518 * Don't allow executable mappings if we can't
519 * indirectly execute the file.
521 if (prot
& VM_PROT_EXECUTE
) {
528 * Strip the executable bit from 'maxprot' to make sure
529 * it can't be made executable later.
531 maxprot
&= ~VM_PROT_EXECUTE
;
534 #endif /* NVERIEXEC > 0 */
537 pax_mprotect(l
, &prot
, &maxprot
);
538 #endif /* PAX_MPROTECT */
541 pax_aslr(l
, &addr
, orig_addr
, flags
);
542 #endif /* PAX_ASLR */
545 * now let kernel internal function uvm_mmap do the work.
548 error
= uvm_mmap(&p
->p_vmspace
->vm_map
, &addr
, size
, prot
, maxprot
,
549 flags
, handle
, pos
, p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
);
552 /* remember to add offset */
553 *retval
= (register_t
)(addr
+ pageoff
);
562 * sys___msync13: the msync system call (a front-end for flush)
566 sys___msync13(struct lwp
*l
, const struct sys___msync13_args
*uap
,
570 syscallarg(void *) addr;
571 syscallarg(size_t) len;
572 syscallarg(int) flags;
574 struct proc
*p
= l
->l_proc
;
576 vsize_t size
, pageoff
;
578 int error
, rv
, flags
, uvmflags
;
581 * extract syscall args from the uap
584 addr
= (vaddr_t
)SCARG(uap
, addr
);
585 size
= (vsize_t
)SCARG(uap
, len
);
586 flags
= SCARG(uap
, flags
);
588 /* sanity check flags */
589 if ((flags
& ~(MS_ASYNC
| MS_SYNC
| MS_INVALIDATE
)) != 0 ||
590 (flags
& (MS_ASYNC
| MS_SYNC
| MS_INVALIDATE
)) == 0 ||
591 (flags
& (MS_ASYNC
| MS_SYNC
)) == (MS_ASYNC
| MS_SYNC
))
593 if ((flags
& (MS_ASYNC
| MS_SYNC
)) == 0)
597 * align the address to a page boundary and adjust the size accordingly.
600 pageoff
= (addr
& PAGE_MASK
);
603 size
= (vsize_t
)round_page(size
);
605 error
= range_test(addr
, size
, false);
613 map
= &p
->p_vmspace
->vm_map
;
616 * XXXCDC: do we really need this semantic?
618 * XXX Gak! If size is zero we are supposed to sync "all modified
619 * pages with the region containing addr". Unfortunately, we
620 * don't really keep track of individual mmaps so we approximate
621 * by flushing the range of the map entry containing addr.
622 * This can be incorrect if the region splits or is coalesced
627 struct vm_map_entry
*entry
;
629 vm_map_lock_read(map
);
630 rv
= uvm_map_lookup_entry(map
, addr
, &entry
);
633 size
= entry
->end
- entry
->start
;
635 vm_map_unlock_read(map
);
641 * translate MS_ flags into PGO_ flags
644 uvmflags
= PGO_CLEANIT
;
645 if (flags
& MS_INVALIDATE
)
646 uvmflags
|= PGO_FREE
;
648 uvmflags
|= PGO_SYNCIO
;
650 error
= uvm_map_clean(map
, addr
, addr
+size
, uvmflags
);
655 * sys_munmap: unmap a users memory
659 sys_munmap(struct lwp
*l
, const struct sys_munmap_args
*uap
, register_t
*retval
)
662 syscallarg(void *) addr;
663 syscallarg(size_t) len;
665 struct proc
*p
= l
->l_proc
;
667 vsize_t size
, pageoff
;
669 struct vm_map_entry
*dead_entries
;
676 addr
= (vaddr_t
)SCARG(uap
, addr
);
677 size
= (vsize_t
)SCARG(uap
, len
);
680 * align the address to a page boundary and adjust the size accordingly.
683 pageoff
= (addr
& PAGE_MASK
);
686 size
= (vsize_t
)round_page(size
);
691 error
= range_test(addr
, size
, false);
695 map
= &p
->p_vmspace
->vm_map
;
698 * interesting system call semantic: make sure entire range is
699 * allocated before allowing an unmap.
704 if (!uvm_map_checkprot(map
, addr
, addr
+ size
, VM_PROT_NONE
)) {
709 uvm_unmap_remove(map
, addr
, addr
+ size
, &dead_entries
, NULL
, 0);
711 if (dead_entries
!= NULL
)
712 uvm_unmap_detach(dead_entries
, 0);
717 * sys_mprotect: the mprotect system call
721 sys_mprotect(struct lwp
*l
, const struct sys_mprotect_args
*uap
,
725 syscallarg(void *) addr;
726 syscallarg(size_t) len;
727 syscallarg(int) prot;
729 struct proc
*p
= l
->l_proc
;
731 vsize_t size
, pageoff
;
736 * extract syscall args from uap
739 addr
= (vaddr_t
)SCARG(uap
, addr
);
740 size
= (vsize_t
)SCARG(uap
, len
);
741 prot
= SCARG(uap
, prot
) & VM_PROT_ALL
;
744 * align the address to a page boundary and adjust the size accordingly.
747 pageoff
= (addr
& PAGE_MASK
);
750 size
= round_page(size
);
752 error
= range_test(addr
, size
, false);
756 error
= uvm_map_protect(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
, prot
,
762 * sys_minherit: the minherit system call
766 sys_minherit(struct lwp
*l
, const struct sys_minherit_args
*uap
,
770 syscallarg(void *) addr;
772 syscallarg(int) inherit;
774 struct proc
*p
= l
->l_proc
;
776 vsize_t size
, pageoff
;
777 vm_inherit_t inherit
;
780 addr
= (vaddr_t
)SCARG(uap
, addr
);
781 size
= (vsize_t
)SCARG(uap
, len
);
782 inherit
= SCARG(uap
, inherit
);
785 * align the address to a page boundary and adjust the size accordingly.
788 pageoff
= (addr
& PAGE_MASK
);
791 size
= (vsize_t
)round_page(size
);
793 error
= range_test(addr
, size
, false);
797 error
= uvm_map_inherit(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
,
803 * sys_madvise: give advice about memory usage.
808 sys_madvise(struct lwp
*l
, const struct sys_madvise_args
*uap
,
812 syscallarg(void *) addr;
813 syscallarg(size_t) len;
814 syscallarg(int) behav;
816 struct proc
*p
= l
->l_proc
;
818 vsize_t size
, pageoff
;
821 addr
= (vaddr_t
)SCARG(uap
, addr
);
822 size
= (vsize_t
)SCARG(uap
, len
);
823 advice
= SCARG(uap
, behav
);
826 * align the address to a page boundary, and adjust the size accordingly
829 pageoff
= (addr
& PAGE_MASK
);
832 size
= (vsize_t
)round_page(size
);
834 error
= range_test(addr
, size
, false);
841 case MADV_SEQUENTIAL
:
842 error
= uvm_map_advice(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
,
849 * Activate all these pages, pre-faulting them in if
852 error
= uvm_map_willneed(&p
->p_vmspace
->vm_map
,
859 * Deactivate all these pages. We don't need them
860 * any more. We don't, however, toss the data in
864 error
= uvm_map_clean(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
,
871 * These pages contain no valid data, and may be
872 * garbage-collected. Toss all resources, including
873 * any swap space in use.
876 error
= uvm_map_clean(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
,
880 case MADV_SPACEAVAIL
:
883 * XXXMRG What is this? I think it's:
885 * Ensure that we have allocated backing-store
888 * This is going to require changes to the page daemon,
889 * as it will free swap space allocated to pages in core.
890 * There's also what to do for device/file/anonymous memory.
903 * sys_mlock: memory lock
907 sys_mlock(struct lwp
*l
, const struct sys_mlock_args
*uap
, register_t
*retval
)
910 syscallarg(const void *) addr;
911 syscallarg(size_t) len;
913 struct proc
*p
= l
->l_proc
;
915 vsize_t size
, pageoff
;
919 * extract syscall args from uap
922 addr
= (vaddr_t
)SCARG(uap
, addr
);
923 size
= (vsize_t
)SCARG(uap
, len
);
926 * align the address to a page boundary and adjust the size accordingly
929 pageoff
= (addr
& PAGE_MASK
);
932 size
= (vsize_t
)round_page(size
);
934 error
= range_test(addr
, size
, false);
938 if (atop(size
) + uvmexp
.wired
> uvmexp
.wiredmax
)
941 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
942 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
)
945 error
= uvm_map_pageable(&p
->p_vmspace
->vm_map
, addr
, addr
+size
, false,
953 * sys_munlock: unlock wired pages
957 sys_munlock(struct lwp
*l
, const struct sys_munlock_args
*uap
,
961 syscallarg(const void *) addr;
962 syscallarg(size_t) len;
964 struct proc
*p
= l
->l_proc
;
966 vsize_t size
, pageoff
;
970 * extract syscall args from uap
973 addr
= (vaddr_t
)SCARG(uap
, addr
);
974 size
= (vsize_t
)SCARG(uap
, len
);
977 * align the address to a page boundary, and adjust the size accordingly
980 pageoff
= (addr
& PAGE_MASK
);
983 size
= (vsize_t
)round_page(size
);
985 error
= range_test(addr
, size
, false);
989 error
= uvm_map_pageable(&p
->p_vmspace
->vm_map
, addr
, addr
+size
, true,
997 * sys_mlockall: lock all pages mapped into an address space.
1001 sys_mlockall(struct lwp
*l
, const struct sys_mlockall_args
*uap
,
1005 syscallarg(int) flags;
1007 struct proc
*p
= l
->l_proc
;
1010 flags
= SCARG(uap
, flags
);
1013 (flags
& ~(MCL_CURRENT
|MCL_FUTURE
)) != 0)
1016 error
= uvm_map_pageable_all(&p
->p_vmspace
->vm_map
, flags
,
1017 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
);
1022 * sys_munlockall: unlock all pages mapped into an address space.
1026 sys_munlockall(struct lwp
*l
, const void *v
, register_t
*retval
)
1028 struct proc
*p
= l
->l_proc
;
1030 (void) uvm_map_pageable_all(&p
->p_vmspace
->vm_map
, 0, 0);
1035 * uvm_mmap: internal version of mmap
1037 * - used by sys_mmap and various framebuffers
1038 * - handle is a vnode pointer or NULL for MAP_ANON
1039 * - caller must page-align the file offset
1043 uvm_mmap(struct vm_map
*map
, vaddr_t
*addr
, vsize_t size
, vm_prot_t prot
,
1044 vm_prot_t maxprot
, int flags
, void *handle
, voff_t foff
, vsize_t locklimit
)
1046 struct uvm_object
*uobj
;
1050 int advice
= UVM_ADV_NORMAL
;
1051 uvm_flag_t uvmflag
= 0;
1060 if (foff
& PAGE_MASK
)
1062 if ((prot
& maxprot
) != prot
)
1066 * for non-fixed mappings, round off the suggested address.
1067 * for fixed mappings, check alignment and zap old mappings.
1070 if ((flags
& MAP_FIXED
) == 0) {
1071 *addr
= round_page(*addr
);
1073 if (*addr
& PAGE_MASK
)
1075 uvmflag
|= UVM_FLAG_FIXED
;
1076 (void) uvm_unmap(map
, *addr
, *addr
+ size
);
1080 * Try to see if any requested alignment can even be attemped.
1081 * Make sure we can express the alignment (asking for a >= 4GB
1082 * alignment on an ILP32 architecure make no sense) and the
1083 * alignment is at least for a page sized quanitiy. If the
1084 * request was for a fixed mapping, make sure supplied address
1085 * adheres to the request alignment.
1087 align
= (flags
& MAP_ALIGNMENT_MASK
) >> MAP_ALIGNMENT_SHIFT
;
1089 if (align
>= sizeof(vaddr_t
) * NBBY
)
1091 align
= 1L << align
;
1092 if (align
< PAGE_SIZE
)
1094 if (align
>= vm_map_max(map
))
1096 if (flags
& MAP_FIXED
) {
1097 if ((*addr
& (align
-1)) != 0)
1104 * check resource limits
1107 if (!VM_MAP_IS_KERNEL(map
) &&
1108 (((rlim_t
)curproc
->p_vmspace
->vm_map
.size
+ (rlim_t
)size
) >
1109 curproc
->p_rlimit
[RLIMIT_AS
].rlim_cur
))
1113 * handle anon vs. non-anon mappings. for non-anon mappings attach
1114 * to underlying vm object.
1117 if (flags
& MAP_ANON
) {
1118 KASSERT(handle
== NULL
);
1119 foff
= UVM_UNKNOWN_OFFSET
;
1121 if ((flags
& MAP_SHARED
) == 0)
1122 /* XXX: defer amap create */
1123 uvmflag
|= UVM_FLAG_COPYONW
;
1125 /* shared: create amap now */
1126 uvmflag
|= UVM_FLAG_OVERLAY
;
1129 KASSERT(handle
!= NULL
);
1130 vp
= (struct vnode
*)handle
;
1133 * Don't allow mmap for EXEC if the file system
1134 * is mounted NOEXEC.
1136 if ((prot
& PROT_EXEC
) != 0 &&
1137 (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) != 0)
1140 if (vp
->v_type
!= VCHR
) {
1141 error
= VOP_MMAP(vp
, prot
, curlwp
->l_cred
);
1149 * If the vnode is being mapped with PROT_EXEC,
1150 * then mark it as text.
1152 if (prot
& PROT_EXEC
) {
1159 * XXX Some devices don't like to be mapped with
1160 * XXX PROT_EXEC or PROT_WRITE, but we don't really
1161 * XXX have a better way of handling this, right now
1164 uobj
= udv_attach((void *) &vp
->v_rdev
,
1165 (flags
& MAP_SHARED
) ? i
:
1166 (i
& ~VM_PROT_WRITE
), foff
, size
);
1168 } while ((uobj
== NULL
) && (i
> 0));
1171 advice
= UVM_ADV_RANDOM
;
1173 if ((flags
& MAP_SHARED
) == 0) {
1174 uvmflag
|= UVM_FLAG_COPYONW
;
1178 * Set vnode flags to indicate the new kinds of mapping.
1179 * We take the vnode lock in exclusive mode here to serialize
1182 * Safe to check for these flag values without a lock, as
1183 * long as a reference to the vnode is held.
1185 needwritemap
= (vp
->v_iflag
& VI_WRMAP
) == 0 &&
1186 (flags
& MAP_SHARED
) != 0 &&
1187 (maxprot
& VM_PROT_WRITE
) != 0;
1188 if ((vp
->v_vflag
& VV_MAPPED
) == 0 || needwritemap
) {
1189 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1190 vp
->v_vflag
|= VV_MAPPED
;
1192 mutex_enter(&vp
->v_interlock
);
1193 vp
->v_iflag
|= VI_WRMAP
;
1194 mutex_exit(&vp
->v_interlock
);
1200 uvmflag
= UVM_MAPFLAG(prot
, maxprot
,
1201 (flags
& MAP_SHARED
) ? UVM_INH_SHARE
: UVM_INH_COPY
,
1203 error
= uvm_map(map
, addr
, size
, uobj
, foff
, align
, uvmflag
);
1206 uobj
->pgops
->pgo_detach(uobj
);
1211 * POSIX 1003.1b -- if our address space was configured
1212 * to lock all future mappings, wire the one we just made.
1214 * Also handle the MAP_WIRED flag here.
1217 if (prot
== VM_PROT_NONE
) {
1220 * No more work to do in this case.
1225 if ((flags
& MAP_WIRED
) != 0 || (map
->flags
& VM_MAP_WIREFUTURE
) != 0) {
1227 if (atop(size
) + uvmexp
.wired
> uvmexp
.wiredmax
||
1229 size
+ ptoa(pmap_wired_count(vm_map_pmap(map
))) >
1232 uvm_unmap(map
, *addr
, *addr
+ size
);
1237 * uvm_map_pageable() always returns the map unlocked.
1240 error
= uvm_map_pageable(map
, *addr
, *addr
+ size
,
1241 false, UVM_LK_ENTER
);
1243 uvm_unmap(map
, *addr
, *addr
+ size
);
1252 uvm_default_mapaddr(struct proc
*p
, vaddr_t base
, vsize_t sz
)
1255 return VM_DEFAULT_ADDRESS(base
, sz
);