4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
38 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
39 * $FreeBSD: src/sys/vm/vm_mmap.c,v 1.108.2.6 2002/07/02 20:06:19 dillon Exp $
43 * Mapped file (mmap) interface to VM
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/sysproto.h>
50 #include <sys/filedesc.h>
51 #include <sys/kern_syscall.h>
54 #include <sys/resource.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
57 #include <sys/fcntl.h>
62 #include <sys/vmmeter.h>
63 #include <sys/sysctl.h>
66 #include <vm/vm_param.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_kern.h>
78 #include <sys/file2.h>
79 #include <sys/thread.h>
80 #include <sys/thread2.h>
82 static int max_proc_mmap
;
83 SYSCTL_INT(_vm
, OID_AUTO
, max_proc_mmap
, CTLFLAG_RW
, &max_proc_mmap
, 0, "");
85 SYSCTL_INT(_vm
, OID_AUTO
, vkernel_enable
, CTLFLAG_RW
, &vkernel_enable
, 0, "");
88 * Set the maximum number of vm_map_entry structures per process. Roughly
89 * speaking vm_map_entry structures are tiny, so allowing them to eat 1/100
90 * of our KVM malloc space still results in generous limits. We want a
91 * default that is good enough to prevent the kernel running out of resources
92 * if attacked from compromised user account but generous enough such that
93 * multi-threaded processes are not unduly inconvenienced.
96 static void vmmapentry_rsrc_init (void *);
97 SYSINIT(vmmersrc
, SI_BOOT1_POST
, SI_ORDER_ANY
, vmmapentry_rsrc_init
, NULL
)
100 vmmapentry_rsrc_init(void *dummy
)
102 max_proc_mmap
= KvaSize
/ sizeof(struct vm_map_entry
);
103 max_proc_mmap
/= 100;
110 sys_sbrk(struct sbrk_args
*uap
)
112 /* Not yet implemented */
117 * sstk_args(int incr)
122 sys_sstk(struct sstk_args
*uap
)
124 /* Not yet implemented */
129 * mmap_args(void *addr, size_t len, int prot, int flags, int fd,
130 * long pad, off_t pos)
132 * Memory Map (mmap) system call. Note that the file offset
133 * and address are allowed to be NOT page aligned, though if
134 * the MAP_FIXED flag it set, both must have the same remainder
135 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
136 * page-aligned, the actual mapping starts at trunc_page(addr)
137 * and the return value is adjusted up by the page offset.
139 * Generally speaking, only character devices which are themselves
140 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
141 * there would be no cache coherency between a descriptor and a VM mapping
142 * both to the same character device.
144 * Block devices can be mmap'd no matter what they represent. Cache coherency
145 * is maintained as long as you do not write directly to the underlying
151 kern_mmap(struct vmspace
*vms
, caddr_t uaddr
, size_t ulen
,
152 int uprot
, int uflags
, int fd
, off_t upos
, void **res
)
154 struct thread
*td
= curthread
;
155 struct proc
*p
= td
->td_proc
;
156 struct file
*fp
= NULL
;
160 vm_size_t size
, pageoff
;
161 vm_prot_t prot
, maxprot
;
169 addr
= (vm_offset_t
) uaddr
;
171 prot
= uprot
& VM_PROT_ALL
;
176 * Make sure mapping fits into numeric range etc.
178 * NOTE: We support the full unsigned range for size now.
180 if (((flags
& MAP_ANON
) && (fd
!= -1 || pos
!= 0)))
186 if (flags
& MAP_STACK
) {
188 ((prot
& (PROT_READ
| PROT_WRITE
)) != (PROT_READ
| PROT_WRITE
)))
195 * Virtual page tables cannot be used with MAP_STACK. Apart from
196 * it not making any sense, the aux union is used by both
199 * Because the virtual page table is stored in the backing object
200 * and might be updated by the kernel, the mapping must be R+W.
202 if (flags
& MAP_VPAGETABLE
) {
203 if (vkernel_enable
== 0)
205 if (flags
& MAP_STACK
)
207 if ((prot
& (PROT_READ
|PROT_WRITE
)) != (PROT_READ
|PROT_WRITE
))
212 * Align the file position to a page boundary,
213 * and save its page offset component.
215 pageoff
= (pos
& PAGE_MASK
);
218 /* Adjust size for rounding (on both ends). */
219 size
+= pageoff
; /* low end... */
220 size
= (vm_size_t
) round_page(size
); /* hi end */
221 if (size
< ulen
) /* wrap */
225 * Check for illegal addresses. Watch out for address wrap... Note
226 * that VM_*_ADDRESS are not constants due to casts (argh).
228 if (flags
& (MAP_FIXED
| MAP_TRYFIXED
)) {
230 * The specified address must have the same remainder
231 * as the file offset taken modulo PAGE_SIZE, so it
232 * should be aligned after adjustment by pageoff.
235 if (addr
& PAGE_MASK
)
239 * Address range must be all in user VM space and not wrap.
241 tmpaddr
= addr
+ size
;
244 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
)
246 if (VM_MIN_USER_ADDRESS
> 0 && addr
< VM_MIN_USER_ADDRESS
)
250 * Get a hint of where to map. It also provides mmap offset
251 * randomization if enabled.
253 addr
= vm_map_hint(p
, addr
, prot
);
256 if (flags
& MAP_ANON
) {
258 * Mapping blank space is trivial.
261 maxprot
= VM_PROT_ALL
;
264 * Mapping file, get fp for validation. Obtain vnode and make
265 * sure it is of appropriate type.
267 fp
= holdfp(p
->p_fd
, fd
, -1);
270 if (fp
->f_type
!= DTYPE_VNODE
) {
275 * POSIX shared-memory objects are defined to have
276 * kernel persistence, and are not defined to support
277 * read(2)/write(2) -- or even open(2). Thus, we can
278 * use MAP_ASYNC to trade on-disk coherence for speed.
279 * The shm_open(3) library routine turns on the FPOSIXSHM
280 * flag to request this behavior.
282 if (fp
->f_flag
& FPOSIXSHM
)
284 vp
= (struct vnode
*) fp
->f_data
;
287 * Validate the vnode for the operation.
292 * Get the proper underlying object
294 if ((obj
= vp
->v_object
) == NULL
) {
298 KKASSERT((struct vnode
*)obj
->handle
== vp
);
302 * Make sure a device has not been revoked.
303 * Mappability is handled by the device layer.
305 if (vp
->v_rdev
== NULL
) {
312 * Nothing else is mappable.
319 * XXX hack to handle use of /dev/zero to map anon memory (ala
322 if (vp
->v_type
== VCHR
&& iszerodev(vp
->v_rdev
)) {
324 maxprot
= VM_PROT_ALL
;
329 * cdevs does not provide private mappings of any kind.
331 if (vp
->v_type
== VCHR
&&
332 (flags
& (MAP_PRIVATE
|MAP_COPY
))) {
337 * Ensure that file and memory protections are
338 * compatible. Note that we only worry about
339 * writability if mapping is shared; in this case,
340 * current and max prot are dictated by the open file.
341 * XXX use the vnode instead? Problem is: what
342 * credentials do we use for determination? What if
343 * proc does a setuid?
345 maxprot
= VM_PROT_EXECUTE
; /* ??? */
346 if (fp
->f_flag
& FREAD
) {
347 maxprot
|= VM_PROT_READ
;
348 } else if (prot
& PROT_READ
) {
353 * If we are sharing potential changes (either via
354 * MAP_SHARED or via the implicit sharing of character
355 * device mappings), and we are trying to get write
356 * permission although we opened it without asking
357 * for it, bail out. Check for superuser, only if
358 * we're at securelevel < 1, to allow the XIG X server
359 * to continue to work.
361 if ((flags
& MAP_SHARED
) != 0 || vp
->v_type
== VCHR
) {
362 if ((fp
->f_flag
& FWRITE
) != 0) {
364 if ((error
= VOP_GETATTR(vp
, &va
))) {
368 (IMMUTABLE
|APPEND
)) == 0) {
369 maxprot
|= VM_PROT_WRITE
;
370 } else if (prot
& PROT_WRITE
) {
374 } else if ((prot
& PROT_WRITE
) != 0) {
379 maxprot
|= VM_PROT_WRITE
;
385 lwkt_gettoken(&vms
->vm_map
.token
);
388 * Do not allow more then a certain number of vm_map_entry structures
389 * per process. Scale with the number of rforks sharing the map
390 * to make the limit reasonable for threads.
393 vms
->vm_map
.nentries
>= max_proc_mmap
* vms
->vm_sysref
.refcnt
) {
395 lwkt_reltoken(&vms
->vm_map
.token
);
399 error
= vm_mmap(&vms
->vm_map
, &addr
, size
, prot
, maxprot
,
402 *res
= (void *)(addr
+ pageoff
);
404 lwkt_reltoken(&vms
->vm_map
.token
);
413 * mmap system call handler
418 sys_mmap(struct mmap_args
*uap
)
422 error
= kern_mmap(curproc
->p_vmspace
, uap
->addr
, uap
->len
,
423 uap
->prot
, uap
->flags
,
424 uap
->fd
, uap
->pos
, &uap
->sysmsg_resultp
);
430 * msync system call handler
432 * msync_args(void *addr, size_t len, int flags)
437 sys_msync(struct msync_args
*uap
)
439 struct proc
*p
= curproc
;
442 vm_size_t size
, pageoff
;
447 addr
= (vm_offset_t
) uap
->addr
;
451 pageoff
= (addr
& PAGE_MASK
);
454 size
= (vm_size_t
) round_page(size
);
455 if (size
< uap
->len
) /* wrap */
457 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
458 if (tmpaddr
< addr
) /* wrap */
461 if ((flags
& (MS_ASYNC
|MS_INVALIDATE
)) == (MS_ASYNC
|MS_INVALIDATE
))
464 map
= &p
->p_vmspace
->vm_map
;
467 * map->token serializes extracting the address range for size == 0
468 * msyncs with the vm_map_clean call; if the token were not held
469 * across the two calls, an intervening munmap/mmap pair, for example,
470 * could cause msync to occur on a wrong region.
472 lwkt_gettoken(&map
->token
);
475 * XXX Gak! If size is zero we are supposed to sync "all modified
476 * pages with the region containing addr". Unfortunately, we don't
477 * really keep track of individual mmaps so we approximate by flushing
478 * the range of the map entry containing addr. This can be incorrect
479 * if the region splits or is coalesced with a neighbor.
482 vm_map_entry_t entry
;
484 vm_map_lock_read(map
);
485 rv
= vm_map_lookup_entry(map
, addr
, &entry
);
487 vm_map_unlock_read(map
);
488 rv
= KERN_INVALID_ADDRESS
;
492 size
= entry
->end
- entry
->start
;
493 vm_map_unlock_read(map
);
497 * Clean the pages and interpret the return value.
499 rv
= vm_map_clean(map
, addr
, addr
+ size
, (flags
& MS_ASYNC
) == 0,
500 (flags
& MS_INVALIDATE
) != 0);
502 lwkt_reltoken(&map
->token
);
507 case KERN_INVALID_ADDRESS
:
508 return (EINVAL
); /* Sun returns ENOMEM? */
519 * munmap system call handler
521 * munmap_args(void *addr, size_t len)
526 sys_munmap(struct munmap_args
*uap
)
528 struct proc
*p
= curproc
;
531 vm_size_t size
, pageoff
;
534 addr
= (vm_offset_t
) uap
->addr
;
537 pageoff
= (addr
& PAGE_MASK
);
540 size
= (vm_size_t
) round_page(size
);
541 if (size
< uap
->len
) /* wrap */
543 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
544 if (tmpaddr
< addr
) /* wrap */
551 * Check for illegal addresses. Watch out for address wrap... Note
552 * that VM_*_ADDRESS are not constants due to casts (argh).
554 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
)
556 if (VM_MIN_USER_ADDRESS
> 0 && addr
< VM_MIN_USER_ADDRESS
)
559 map
= &p
->p_vmspace
->vm_map
;
561 /* map->token serializes between the map check and the actual unmap */
562 lwkt_gettoken(&map
->token
);
565 * Make sure entire range is allocated.
567 if (!vm_map_check_protection(map
, addr
, addr
+ size
,
568 VM_PROT_NONE
, FALSE
)) {
569 lwkt_reltoken(&map
->token
);
572 /* returns nothing but KERN_SUCCESS anyway */
573 vm_map_remove(map
, addr
, addr
+ size
);
574 lwkt_reltoken(&map
->token
);
579 * mprotect_args(const void *addr, size_t len, int prot)
584 sys_mprotect(struct mprotect_args
*uap
)
586 struct proc
*p
= curproc
;
589 vm_size_t size
, pageoff
;
593 addr
= (vm_offset_t
) uap
->addr
;
595 prot
= uap
->prot
& VM_PROT_ALL
;
596 #if defined(VM_PROT_READ_IS_EXEC)
597 if (prot
& VM_PROT_READ
)
598 prot
|= VM_PROT_EXECUTE
;
601 pageoff
= (addr
& PAGE_MASK
);
604 size
= (vm_size_t
) round_page(size
);
605 if (size
< uap
->len
) /* wrap */
607 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
608 if (tmpaddr
< addr
) /* wrap */
611 switch (vm_map_protect(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
,
616 case KERN_PROTECTION_FAILURE
:
627 * minherit system call handler
629 * minherit_args(void *addr, size_t len, int inherit)
634 sys_minherit(struct minherit_args
*uap
)
636 struct proc
*p
= curproc
;
639 vm_size_t size
, pageoff
;
640 vm_inherit_t inherit
;
643 addr
= (vm_offset_t
)uap
->addr
;
645 inherit
= uap
->inherit
;
647 pageoff
= (addr
& PAGE_MASK
);
650 size
= (vm_size_t
) round_page(size
);
651 if (size
< uap
->len
) /* wrap */
653 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
654 if (tmpaddr
< addr
) /* wrap */
657 switch (vm_map_inherit(&p
->p_vmspace
->vm_map
, addr
,
658 addr
+ size
, inherit
)) {
662 case KERN_PROTECTION_FAILURE
:
673 * madvise system call handler
675 * madvise_args(void *addr, size_t len, int behav)
680 sys_madvise(struct madvise_args
*uap
)
682 struct proc
*p
= curproc
;
683 vm_offset_t start
, end
;
684 vm_offset_t tmpaddr
= (vm_offset_t
)uap
->addr
+ uap
->len
;
688 * Check for illegal behavior
690 if (uap
->behav
< 0 || uap
->behav
>= MADV_CONTROL_END
)
693 * Check for illegal addresses. Watch out for address wrap... Note
694 * that VM_*_ADDRESS are not constants due to casts (argh).
696 if (tmpaddr
< (vm_offset_t
)uap
->addr
)
698 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
)
700 if (VM_MIN_USER_ADDRESS
> 0 && uap
->addr
< VM_MIN_USER_ADDRESS
)
704 * Since this routine is only advisory, we default to conservative
707 start
= trunc_page((vm_offset_t
)uap
->addr
);
708 end
= round_page(tmpaddr
);
710 error
= vm_map_madvise(&p
->p_vmspace
->vm_map
, start
, end
,
716 * mcontrol system call handler
718 * mcontrol_args(void *addr, size_t len, int behav, off_t value)
723 sys_mcontrol(struct mcontrol_args
*uap
)
725 struct proc
*p
= curproc
;
726 vm_offset_t start
, end
;
727 vm_offset_t tmpaddr
= (vm_offset_t
)uap
->addr
+ uap
->len
;
731 * Check for illegal behavior
733 if (uap
->behav
< 0 || uap
->behav
> MADV_CONTROL_END
)
736 * Check for illegal addresses. Watch out for address wrap... Note
737 * that VM_*_ADDRESS are not constants due to casts (argh).
739 if (tmpaddr
< (vm_offset_t
) uap
->addr
)
741 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
)
743 if (VM_MIN_USER_ADDRESS
> 0 && uap
->addr
< VM_MIN_USER_ADDRESS
)
747 * Since this routine is only advisory, we default to conservative
750 start
= trunc_page((vm_offset_t
)uap
->addr
);
751 end
= round_page(tmpaddr
);
753 error
= vm_map_madvise(&p
->p_vmspace
->vm_map
, start
, end
,
754 uap
->behav
, uap
->value
);
760 * mincore system call handler
762 * mincore_args(const void *addr, size_t len, char *vec)
767 sys_mincore(struct mincore_args
*uap
)
769 struct proc
*p
= curproc
;
770 vm_offset_t addr
, first_addr
;
771 vm_offset_t end
, cend
;
776 int vecindex
, lastvecindex
;
777 vm_map_entry_t current
;
778 vm_map_entry_t entry
;
780 unsigned int timestamp
;
783 * Make sure that the addresses presented are valid for user
786 first_addr
= addr
= trunc_page((vm_offset_t
) uap
->addr
);
787 end
= addr
+ (vm_size_t
)round_page(uap
->len
);
790 if (VM_MAX_USER_ADDRESS
> 0 && end
> VM_MAX_USER_ADDRESS
)
794 * Address of byte vector
798 map
= &p
->p_vmspace
->vm_map
;
799 pmap
= vmspace_pmap(p
->p_vmspace
);
801 lwkt_gettoken(&map
->token
);
802 vm_map_lock_read(map
);
804 timestamp
= map
->timestamp
;
806 if (!vm_map_lookup_entry(map
, addr
, &entry
))
810 * Do this on a map entry basis so that if the pages are not
811 * in the current processes address space, we can easily look
812 * up the pages elsewhere.
816 (current
!= &map
->header
) && (current
->start
< end
);
817 current
= current
->next
) {
820 * ignore submaps (for now) or null objects
822 if (current
->maptype
!= VM_MAPTYPE_NORMAL
&&
823 current
->maptype
!= VM_MAPTYPE_VPAGETABLE
) {
826 if (current
->object
.vm_object
== NULL
)
830 * limit this scan to the current map entry and the
831 * limits for the mincore call
833 if (addr
< current
->start
)
834 addr
= current
->start
;
840 * scan this entry one page at a time
842 while (addr
< cend
) {
844 * Check pmap first, it is likely faster, also
845 * it can provide info as to whether we are the
846 * one referencing or modifying the page.
848 * If we have to check the VM object, only mess
849 * around with normal maps. Do not mess around
850 * with virtual page tables (XXX).
852 mincoreinfo
= pmap_mincore(pmap
, addr
);
853 if (mincoreinfo
== 0 &&
854 current
->maptype
== VM_MAPTYPE_NORMAL
) {
860 * calculate the page index into the object
862 offset
= current
->offset
+ (addr
- current
->start
);
863 pindex
= OFF_TO_IDX(offset
);
866 * if the page is resident, then gather
867 * information about it. spl protection is
868 * required to maintain the object
869 * association. And XXX what if the page is
870 * busy? What's the deal with that?
872 * XXX vm_token - legacy for pmap_ts_referenced
873 * in i386 and vkernel pmap code.
875 lwkt_gettoken(&vm_token
);
876 vm_object_hold(current
->object
.vm_object
);
877 m
= vm_page_lookup(current
->object
.vm_object
,
880 mincoreinfo
= MINCORE_INCORE
;
883 mincoreinfo
|= MINCORE_MODIFIED_OTHER
;
884 if ((m
->flags
& PG_REFERENCED
) ||
885 pmap_ts_referenced(m
)) {
886 vm_page_flag_set(m
, PG_REFERENCED
);
887 mincoreinfo
|= MINCORE_REFERENCED_OTHER
;
890 vm_object_drop(current
->object
.vm_object
);
891 lwkt_reltoken(&vm_token
);
895 * subyte may page fault. In case it needs to modify
896 * the map, we release the lock.
898 vm_map_unlock_read(map
);
901 * calculate index into user supplied byte vector
903 vecindex
= OFF_TO_IDX(addr
- first_addr
);
906 * If we have skipped map entries, we need to make sure that
907 * the byte vector is zeroed for those skipped entries.
909 while((lastvecindex
+ 1) < vecindex
) {
910 error
= subyte( vec
+ lastvecindex
, 0);
919 * Pass the page information to the user
921 error
= subyte( vec
+ vecindex
, mincoreinfo
);
928 * If the map has changed, due to the subyte, the previous
929 * output may be invalid.
931 vm_map_lock_read(map
);
932 if (timestamp
!= map
->timestamp
)
935 lastvecindex
= vecindex
;
941 * subyte may page fault. In case it needs to modify
942 * the map, we release the lock.
944 vm_map_unlock_read(map
);
947 * Zero the last entries in the byte vector.
949 vecindex
= OFF_TO_IDX(end
- first_addr
);
950 while((lastvecindex
+ 1) < vecindex
) {
951 error
= subyte( vec
+ lastvecindex
, 0);
960 * If the map has changed, due to the subyte, the previous
961 * output may be invalid.
963 vm_map_lock_read(map
);
964 if (timestamp
!= map
->timestamp
)
966 vm_map_unlock_read(map
);
970 lwkt_reltoken(&map
->token
);
975 * mlock system call handler
977 * mlock_args(const void *addr, size_t len)
982 sys_mlock(struct mlock_args
*uap
)
986 vm_size_t size
, pageoff
;
987 struct thread
*td
= curthread
;
988 struct proc
*p
= td
->td_proc
;
991 addr
= (vm_offset_t
) uap
->addr
;
994 pageoff
= (addr
& PAGE_MASK
);
997 size
= (vm_size_t
) round_page(size
);
998 if (size
< uap
->len
) /* wrap */
1000 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
1001 if (tmpaddr
< addr
) /* wrap */
1004 if (atop(size
) + vmstats
.v_wire_count
> vm_page_max_wired
)
1008 * We do not need to synchronize against other threads updating ucred;
1009 * they update p->ucred, which is synchronized into td_ucred ourselves.
1011 #ifdef pmap_wired_count
1012 if (size
+ ptoa(pmap_wired_count(vm_map_pmap(&p
->p_vmspace
->vm_map
))) >
1013 p
->p_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
) {
1017 error
= priv_check_cred(td
->td_ucred
, PRIV_ROOT
, 0);
1022 error
= vm_map_unwire(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
, FALSE
);
1023 return (error
== KERN_SUCCESS
? 0 : ENOMEM
);
1032 sys_mlockall(struct mlockall_args
*uap
)
1034 #ifdef _P1003_1B_VISIBLE
1035 struct thread
*td
= curthread
;
1036 struct proc
*p
= td
->td_proc
;
1037 vm_map_t map
= &p
->p_vmspace
->vm_map
;
1038 vm_map_entry_t entry
;
1040 int rc
= KERN_SUCCESS
;
1042 if (((how
& MCL_CURRENT
) == 0) && ((how
& MCL_FUTURE
) == 0))
1045 rc
= priv_check_cred(td
->td_ucred
, PRIV_ROOT
, 0);
1051 if (how
& MCL_CURRENT
) {
1052 for(entry
= map
->header
.next
;
1053 entry
!= &map
->header
;
1054 entry
= entry
->next
);
1060 if (how
& MCL_FUTURE
)
1061 map
->flags
|= MAP_WIREFUTURE
;
1066 #else /* !_P1003_1B_VISIBLE */
1068 #endif /* _P1003_1B_VISIBLE */
1074 * Unwire all user-wired map entries, cancel MCL_FUTURE.
1079 sys_munlockall(struct munlockall_args
*uap
)
1081 struct thread
*td
= curthread
;
1082 struct proc
*p
= td
->td_proc
;
1083 vm_map_t map
= &p
->p_vmspace
->vm_map
;
1084 vm_map_entry_t entry
;
1085 int rc
= KERN_SUCCESS
;
1089 /* Clear MAP_WIREFUTURE to cancel mlockall(MCL_FUTURE) */
1090 map
->flags
&= ~MAP_WIREFUTURE
;
1093 for (entry
= map
->header
.next
;
1094 entry
!= &map
->header
;
1095 entry
= entry
->next
) {
1096 if ((entry
->eflags
& MAP_ENTRY_USER_WIRED
) == 0)
1100 * If we encounter an in-transition entry, we release the
1101 * map lock and retry the scan; we do not decrement any
1102 * wired_count more than once because we do not touch
1103 * any entries with MAP_ENTRY_USER_WIRED not set.
1105 * There is a potential interleaving with concurrent
1106 * mlockall()s here -- if we abort a scan, an mlockall()
1107 * could start, wire a number of entries before our
1108 * current position in, and then stall itself on this
1109 * or any other in-transition entry. If that occurs, when
1110 * we resume, we will unwire those entries.
1112 if (entry
->eflags
& MAP_ENTRY_IN_TRANSITION
) {
1113 entry
->eflags
|= MAP_ENTRY_NEEDS_WAKEUP
;
1114 ++mycpu
->gd_cnt
.v_intrans_coll
;
1115 ++mycpu
->gd_cnt
.v_intrans_wait
;
1116 vm_map_transition_wait(map
);
1120 KASSERT(entry
->wired_count
> 0,
1121 ("wired_count was 0 with USER_WIRED set! %p", entry
));
1123 /* Drop wired count, if it hits zero, unwire the entry */
1124 entry
->eflags
&= ~MAP_ENTRY_USER_WIRED
;
1125 entry
->wired_count
--;
1126 if (entry
->wired_count
== 0)
1127 vm_fault_unwire(map
, entry
);
1137 * munlock system call handler
1139 * munlock_args(const void *addr, size_t len)
1144 sys_munlock(struct munlock_args
*uap
)
1146 struct thread
*td
= curthread
;
1147 struct proc
*p
= td
->td_proc
;
1149 vm_offset_t tmpaddr
;
1150 vm_size_t size
, pageoff
;
1153 addr
= (vm_offset_t
) uap
->addr
;
1156 pageoff
= (addr
& PAGE_MASK
);
1159 size
= (vm_size_t
) round_page(size
);
1161 tmpaddr
= addr
+ size
;
1162 if (tmpaddr
< addr
) /* wrap */
1165 #ifndef pmap_wired_count
1166 error
= priv_check(td
, PRIV_ROOT
);
1171 error
= vm_map_unwire(&p
->p_vmspace
->vm_map
, addr
, addr
+ size
, TRUE
);
1172 return (error
== KERN_SUCCESS
? 0 : ENOMEM
);
1176 * Internal version of mmap.
1177 * Currently used by mmap, exec, and sys5 shared memory.
1178 * Handle is either a vnode pointer or NULL for MAP_ANON.
1183 vm_mmap(vm_map_t map
, vm_offset_t
*addr
, vm_size_t size
, vm_prot_t prot
,
1184 vm_prot_t maxprot
, int flags
, void *handle
, vm_ooffset_t foff
)
1192 struct thread
*td
= curthread
;
1194 int rv
= KERN_SUCCESS
;
1201 objsize
= round_page(size
);
1206 lwkt_gettoken(&map
->token
);
1209 * XXX messy code, fixme
1211 * NOTE: Overflow checks require discrete statements or GCC4
1212 * will optimize it out.
1214 if ((p
= curproc
) != NULL
&& map
== &p
->p_vmspace
->vm_map
) {
1215 esize
= map
->size
+ size
; /* workaround gcc4 opt */
1216 if (esize
< map
->size
||
1217 esize
> p
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
1218 lwkt_reltoken(&map
->token
);
1224 * We currently can only deal with page aligned file offsets.
1225 * The check is here rather than in the syscall because the
1226 * kernel calls this function internally for other mmaping
1227 * operations (such as in exec) and non-aligned offsets will
1228 * cause pmap inconsistencies...so we want to be sure to
1229 * disallow this in all cases.
1231 * NOTE: Overflow checks require discrete statements or GCC4
1232 * will optimize it out.
1234 if (foff
& PAGE_MASK
) {
1235 lwkt_reltoken(&map
->token
);
1240 * Handle alignment. For large memory maps it is possible
1241 * that the MMU can optimize the page table so align anything
1242 * that is a multiple of SEG_SIZE to SEG_SIZE.
1244 * Also align any large mapping (bigger than 16x SG_SIZE) to a
1245 * SEG_SIZE address boundary.
1247 if (flags
& MAP_SIZEALIGN
) {
1249 if ((align
^ (align
- 1)) != (align
<< 1) - 1) {
1250 lwkt_reltoken(&map
->token
);
1253 } else if ((flags
& MAP_FIXED
) == 0 &&
1254 ((size
& SEG_MASK
) == 0 || size
> SEG_SIZE
* 16)) {
1260 if ((flags
& (MAP_FIXED
| MAP_TRYFIXED
)) == 0) {
1262 *addr
= round_page(*addr
);
1264 if (*addr
!= trunc_page(*addr
)) {
1265 lwkt_reltoken(&map
->token
);
1268 eaddr
= *addr
+ size
;
1269 if (eaddr
< *addr
) {
1270 lwkt_reltoken(&map
->token
);
1274 if ((flags
& MAP_TRYFIXED
) == 0)
1275 vm_map_remove(map
, *addr
, *addr
+ size
);
1279 * Lookup/allocate object.
1281 if (flags
& MAP_ANON
) {
1283 * Unnamed anonymous regions always start at 0.
1287 * Default memory object
1289 object
= default_pager_alloc(handle
, objsize
,
1291 if (object
== NULL
) {
1292 lwkt_reltoken(&map
->token
);
1295 docow
= MAP_PREFAULT_PARTIAL
;
1298 * Implicit single instance of a default memory
1299 * object, so we don't need a VM object yet.
1307 vp
= (struct vnode
*)handle
;
1308 if (vp
->v_type
== VCHR
) {
1310 * Device mappings (device size unknown?).
1311 * Force them to be shared.
1313 handle
= (void *)(intptr_t)vp
->v_rdev
;
1314 object
= dev_pager_alloc(handle
, objsize
, prot
, foff
);
1315 if (object
== NULL
) {
1316 lwkt_reltoken(&map
->token
);
1319 docow
= MAP_PREFAULT_PARTIAL
;
1320 flags
&= ~(MAP_PRIVATE
|MAP_COPY
);
1321 flags
|= MAP_SHARED
;
1324 * Regular file mapping (typically). The attribute
1325 * check is for the link count test only. Mmapble
1326 * vnodes must already have a VM object assigned.
1331 error
= VOP_GETATTR(vp
, &vat
);
1333 lwkt_reltoken(&map
->token
);
1336 docow
= MAP_PREFAULT_PARTIAL
;
1337 object
= vnode_pager_reference(vp
);
1338 if (object
== NULL
&& vp
->v_type
== VREG
) {
1339 lwkt_reltoken(&map
->token
);
1340 kprintf("Warning: cannot mmap vnode %p, no "
1346 * If it is a regular file without any references
1347 * we do not need to sync it.
1349 if (vp
->v_type
== VREG
&& vat
.va_nlink
== 0) {
1350 flags
|= MAP_NOSYNC
;
1356 * Deal with the adjusted flags
1358 if ((flags
& (MAP_ANON
|MAP_SHARED
)) == 0)
1359 docow
|= MAP_COPY_ON_WRITE
;
1360 if (flags
& MAP_NOSYNC
)
1361 docow
|= MAP_DISABLE_SYNCER
;
1362 if (flags
& MAP_NOCORE
)
1363 docow
|= MAP_DISABLE_COREDUMP
;
1365 #if defined(VM_PROT_READ_IS_EXEC)
1366 if (prot
& VM_PROT_READ
)
1367 prot
|= VM_PROT_EXECUTE
;
1369 if (maxprot
& VM_PROT_READ
)
1370 maxprot
|= VM_PROT_EXECUTE
;
1374 * This may place the area in its own page directory if (size) is
1375 * large enough, otherwise it typically returns its argument.
1378 *addr
= pmap_addr_hint(object
, *addr
, size
);
1382 * Stack mappings need special attention.
1384 * Mappings that use virtual page tables will default to storing
1385 * the page table at offset 0.
1387 if (flags
& MAP_STACK
) {
1388 rv
= vm_map_stack(map
, *addr
, size
, flags
,
1389 prot
, maxprot
, docow
);
1390 } else if (flags
& MAP_VPAGETABLE
) {
1391 rv
= vm_map_find(map
, object
, foff
, addr
, size
, align
,
1392 fitit
, VM_MAPTYPE_VPAGETABLE
,
1393 prot
, maxprot
, docow
);
1395 rv
= vm_map_find(map
, object
, foff
, addr
, size
, align
,
1396 fitit
, VM_MAPTYPE_NORMAL
,
1397 prot
, maxprot
, docow
);
1400 if (rv
!= KERN_SUCCESS
) {
1402 * Lose the object reference. Will destroy the
1403 * object if it's an unnamed anonymous mapping
1404 * or named anonymous without other references.
1406 vm_object_deallocate(object
);
1411 * Shared memory is also shared with children.
1413 if (flags
& (MAP_SHARED
|MAP_INHERIT
)) {
1414 rv
= vm_map_inherit(map
, *addr
, *addr
+ size
, VM_INHERIT_SHARE
);
1415 if (rv
!= KERN_SUCCESS
) {
1416 vm_map_remove(map
, *addr
, *addr
+ size
);
1421 /* If a process has marked all future mappings for wiring, do so */
1422 if ((rv
== KERN_SUCCESS
) && (map
->flags
& MAP_WIREFUTURE
))
1423 vm_map_unwire(map
, *addr
, *addr
+ size
, FALSE
);
1426 * Set the access time on the vnode
1429 vn_mark_atime(vp
, td
);
1431 lwkt_reltoken(&map
->token
);
1436 case KERN_INVALID_ADDRESS
:
1439 case KERN_PROTECTION_FAILURE
: