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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
36 * @(#)vm_mmap.c 8.4 (Berkeley) 1/12/94
40 * Mapped file (mmap) interface to VM
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "opt_compat.h"
47 #include "opt_hwpmc_hooks.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capability.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/filedesc.h>
59 #include <sys/racct.h>
60 #include <sys/resource.h>
61 #include <sys/resourcevar.h>
62 #include <sys/rwlock.h>
63 #include <sys/sysctl.h>
64 #include <sys/vnode.h>
65 #include <sys/fcntl.h>
68 #include <sys/mount.h>
71 #include <sys/sysent.h>
72 #include <sys/vmmeter.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_param.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vm_pageout.h>
84 #include <vm/vm_extern.h>
85 #include <vm/vm_page.h>
86 #include <vm/vnode_pager.h>
89 #include <sys/pmckern.h>
93 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RW | CTLFLAG_TUN, &old_mlock, 0,
94 "Do not apply RLIMIT_MEMLOCK on mlockall");
95 TUNABLE_INT("vm.old_mlock", &old_mlock);
97 #ifndef _SYS_SYSPROTO_H_
103 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
104 int *, struct vnode *, vm_ooffset_t *, vm_object_t *, boolean_t *);
105 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
106 int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
107 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
108 int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
117 struct sbrk_args *uap;
119 /* Not yet implemented */
123 #ifndef _SYS_SYSPROTO_H_
136 struct sstk_args *uap;
138 /* Not yet implemented */
142 #if defined(COMPAT_43)
143 #ifndef _SYS_SYSPROTO_H_
144 struct getpagesize_args {
150 ogetpagesize(td, uap)
152 struct getpagesize_args *uap;
155 td->td_retval[0] = PAGE_SIZE;
158 #endif /* COMPAT_43 */
162 * Memory Map (mmap) system call. Note that the file offset
163 * and address are allowed to be NOT page aligned, though if
164 * the MAP_FIXED flag it set, both must have the same remainder
165 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
166 * page-aligned, the actual mapping starts at trunc_page(addr)
167 * and the return value is adjusted up by the page offset.
169 * Generally speaking, only character devices which are themselves
170 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
171 * there would be no cache coherency between a descriptor and a VM mapping
172 * both to the same character device.
174 #ifndef _SYS_SYSPROTO_H_
192 struct mmap_args *uap;
195 struct pmckern_map_in pkm;
200 vm_size_t size, pageoff;
201 vm_prot_t cap_maxprot, prot, maxprot;
203 objtype_t handle_type;
204 int align, error, flags;
206 struct vmspace *vms = td->td_proc->p_vmspace;
209 addr = (vm_offset_t) uap->addr;
211 prot = uap->prot & VM_PROT_ALL;
218 * Enforce the constraints.
219 * Mapping of length 0 is only allowed for old binaries.
220 * Anonymous mapping shall specify -1 as filedescriptor and
221 * zero position for new code. Be nice to ancient a.out
222 * binaries and correct pos for anonymous mapping, since old
223 * ld.so sometimes issues anonymous map requests with non-zero
226 if (!SV_CURPROC_FLAG(SV_AOUT)) {
227 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
228 ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
231 if ((flags & MAP_ANON) != 0)
235 if (flags & MAP_STACK) {
236 if ((uap->fd != -1) ||
237 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
244 * Align the file position to a page boundary,
245 * and save its page offset component.
247 pageoff = (pos & PAGE_MASK);
250 /* Adjust size for rounding (on both ends). */
251 size += pageoff; /* low end... */
252 size = (vm_size_t) round_page(size); /* hi end */
254 /* Ensure alignment is at least a page and fits in a pointer. */
255 align = flags & MAP_ALIGNMENT_MASK;
256 if (align != 0 && align != MAP_ALIGNED_SUPER &&
257 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
258 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
262 * Check for illegal addresses. Watch out for address wrap... Note
263 * that VM_*_ADDRESS are not constants due to casts (argh).
265 if (flags & MAP_FIXED) {
267 * The specified address must have the same remainder
268 * as the file offset taken modulo PAGE_SIZE, so it
269 * should be aligned after adjustment by pageoff.
272 if (addr & PAGE_MASK)
275 /* Address range must be all in user VM space. */
276 if (addr < vm_map_min(&vms->vm_map) ||
277 addr + size > vm_map_max(&vms->vm_map))
279 if (addr + size < addr)
283 * XXX for non-fixed mappings where no hint is provided or
284 * the hint would fall in the potential heap space,
285 * place it after the end of the largest possible heap.
287 * There should really be a pmap call to determine a reasonable
290 PROC_LOCK(td->td_proc);
292 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
293 addr < round_page((vm_offset_t)vms->vm_daddr +
294 lim_max(td->td_proc, RLIMIT_DATA))))
295 addr = round_page((vm_offset_t)vms->vm_daddr +
296 lim_max(td->td_proc, RLIMIT_DATA));
297 PROC_UNLOCK(td->td_proc);
299 if (flags & MAP_ANON) {
301 * Mapping blank space is trivial.
304 handle_type = OBJT_DEFAULT;
305 maxprot = VM_PROT_ALL;
306 cap_maxprot = VM_PROT_ALL;
309 * Mapping file, get fp for validation and don't let the
310 * descriptor disappear on us if we block. Check capability
311 * rights, but also return the maximum rights to be combined
312 * with maxprot later.
315 if (prot & PROT_READ)
316 rights |= CAP_MMAP_R;
317 if ((flags & MAP_SHARED) != 0) {
318 if (prot & PROT_WRITE)
319 rights |= CAP_MMAP_W;
321 if (prot & PROT_EXEC)
322 rights |= CAP_MMAP_X;
323 if ((error = fget_mmap(td, uap->fd, rights, &cap_maxprot,
326 if (fp->f_type == DTYPE_SHM) {
328 handle_type = OBJT_SWAP;
329 maxprot = VM_PROT_NONE;
331 /* FREAD should always be set. */
332 if (fp->f_flag & FREAD)
333 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
334 if (fp->f_flag & FWRITE)
335 maxprot |= VM_PROT_WRITE;
338 if (fp->f_type != DTYPE_VNODE) {
342 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
343 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
345 * POSIX shared-memory objects are defined to have
346 * kernel persistence, and are not defined to support
347 * read(2)/write(2) -- or even open(2). Thus, we can
348 * use MAP_ASYNC to trade on-disk coherence for speed.
349 * The shm_open(3) library routine turns on the FPOSIXSHM
350 * flag to request this behavior.
352 if (fp->f_flag & FPOSIXSHM)
357 * Ensure that file and memory protections are
358 * compatible. Note that we only worry about
359 * writability if mapping is shared; in this case,
360 * current and max prot are dictated by the open file.
361 * XXX use the vnode instead? Problem is: what
362 * credentials do we use for determination? What if
363 * proc does a setuid?
365 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
366 maxprot = VM_PROT_NONE;
368 maxprot = VM_PROT_EXECUTE;
369 if (fp->f_flag & FREAD) {
370 maxprot |= VM_PROT_READ;
371 } else if (prot & PROT_READ) {
376 * If we are sharing potential changes (either via
377 * MAP_SHARED or via the implicit sharing of character
378 * device mappings), and we are trying to get write
379 * permission although we opened it without asking
382 if ((flags & MAP_SHARED) != 0) {
383 if ((fp->f_flag & FWRITE) != 0) {
384 maxprot |= VM_PROT_WRITE;
385 } else if ((prot & PROT_WRITE) != 0) {
389 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
390 maxprot |= VM_PROT_WRITE;
391 cap_maxprot |= VM_PROT_WRITE;
394 handle_type = OBJT_VNODE;
398 maxprot &= cap_maxprot;
399 error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
400 flags, handle_type, handle, pos);
403 /* inform hwpmc(4) if an executable is being mapped */
404 if (error == 0 && handle_type == OBJT_VNODE &&
405 (prot & PROT_EXEC)) {
406 pkm.pm_file = handle;
407 pkm.pm_address = (uintptr_t) addr;
408 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
412 td->td_retval[0] = (register_t) (addr + pageoff);
421 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
423 struct mmap_args oargs;
425 oargs.addr = uap->addr;
426 oargs.len = uap->len;
427 oargs.prot = uap->prot;
428 oargs.flags = uap->flags;
430 oargs.pos = uap->pos;
431 return (sys_mmap(td, &oargs));
435 #ifndef _SYS_SYSPROTO_H_
448 struct ommap_args *uap;
450 struct mmap_args nargs;
451 static const char cvtbsdprot[8] = {
455 PROT_EXEC | PROT_WRITE,
457 PROT_EXEC | PROT_READ,
458 PROT_WRITE | PROT_READ,
459 PROT_EXEC | PROT_WRITE | PROT_READ,
462 #define OMAP_ANON 0x0002
463 #define OMAP_COPY 0x0020
464 #define OMAP_SHARED 0x0010
465 #define OMAP_FIXED 0x0100
467 nargs.addr = uap->addr;
468 nargs.len = uap->len;
469 nargs.prot = cvtbsdprot[uap->prot & 0x7];
470 #ifdef COMPAT_FREEBSD32
471 #if defined(__amd64__) || defined(__ia64__)
472 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
474 nargs.prot |= PROT_EXEC;
478 if (uap->flags & OMAP_ANON)
479 nargs.flags |= MAP_ANON;
480 if (uap->flags & OMAP_COPY)
481 nargs.flags |= MAP_COPY;
482 if (uap->flags & OMAP_SHARED)
483 nargs.flags |= MAP_SHARED;
485 nargs.flags |= MAP_PRIVATE;
486 if (uap->flags & OMAP_FIXED)
487 nargs.flags |= MAP_FIXED;
489 nargs.pos = uap->pos;
490 return (sys_mmap(td, &nargs));
492 #endif /* COMPAT_43 */
495 #ifndef _SYS_SYSPROTO_H_
508 struct msync_args *uap;
511 vm_size_t size, pageoff;
516 addr = (vm_offset_t) uap->addr;
520 pageoff = (addr & PAGE_MASK);
523 size = (vm_size_t) round_page(size);
524 if (addr + size < addr)
527 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
530 map = &td->td_proc->p_vmspace->vm_map;
533 * Clean the pages and interpret the return value.
535 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
536 (flags & MS_INVALIDATE) != 0);
540 case KERN_INVALID_ADDRESS:
541 return (EINVAL); /* Sun returns ENOMEM? */
542 case KERN_INVALID_ARGUMENT:
551 #ifndef _SYS_SYSPROTO_H_
563 struct munmap_args *uap;
566 struct pmckern_map_out pkm;
567 vm_map_entry_t entry;
570 vm_size_t size, pageoff;
573 addr = (vm_offset_t) uap->addr;
578 pageoff = (addr & PAGE_MASK);
581 size = (vm_size_t) round_page(size);
582 if (addr + size < addr)
586 * Check for illegal addresses. Watch out for address wrap...
588 map = &td->td_proc->p_vmspace->vm_map;
589 if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
594 * Inform hwpmc if the address range being unmapped contains
595 * an executable region.
597 pkm.pm_address = (uintptr_t) NULL;
598 if (vm_map_lookup_entry(map, addr, &entry)) {
600 entry != &map->header && entry->start < addr + size;
601 entry = entry->next) {
602 if (vm_map_check_protection(map, entry->start,
603 entry->end, VM_PROT_EXECUTE) == TRUE) {
604 pkm.pm_address = (uintptr_t) addr;
605 pkm.pm_size = (size_t) size;
611 vm_map_delete(map, addr, addr + size);
614 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
615 vm_map_lock_downgrade(map);
616 if (pkm.pm_address != (uintptr_t) NULL)
617 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
618 vm_map_unlock_read(map);
622 /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
626 #ifndef _SYS_SYSPROTO_H_
627 struct mprotect_args {
637 sys_mprotect(td, uap)
639 struct mprotect_args *uap;
642 vm_size_t size, pageoff;
645 addr = (vm_offset_t) uap->addr;
647 prot = uap->prot & VM_PROT_ALL;
649 pageoff = (addr & PAGE_MASK);
652 size = (vm_size_t) round_page(size);
653 if (addr + size < addr)
656 switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
657 addr + size, prot, FALSE)) {
660 case KERN_PROTECTION_FAILURE:
662 case KERN_RESOURCE_SHORTAGE:
668 #ifndef _SYS_SYSPROTO_H_
669 struct minherit_args {
679 sys_minherit(td, uap)
681 struct minherit_args *uap;
684 vm_size_t size, pageoff;
685 vm_inherit_t inherit;
687 addr = (vm_offset_t)uap->addr;
689 inherit = uap->inherit;
691 pageoff = (addr & PAGE_MASK);
694 size = (vm_size_t) round_page(size);
695 if (addr + size < addr)
698 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
699 addr + size, inherit)) {
702 case KERN_PROTECTION_FAILURE:
708 #ifndef _SYS_SYSPROTO_H_
709 struct madvise_args {
722 struct madvise_args *uap;
724 vm_offset_t start, end;
730 * Check for our special case, advising the swap pager we are
733 if (uap->behav == MADV_PROTECT) {
734 error = priv_check(td, PRIV_VM_MADV_PROTECT);
738 p->p_flag |= P_PROTECTED;
744 * Check for illegal behavior
746 if (uap->behav < 0 || uap->behav > MADV_CORE)
749 * Check for illegal addresses. Watch out for address wrap... Note
750 * that VM_*_ADDRESS are not constants due to casts (argh).
752 map = &td->td_proc->p_vmspace->vm_map;
753 if ((vm_offset_t)uap->addr < vm_map_min(map) ||
754 (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
756 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
760 * Since this routine is only advisory, we default to conservative
763 start = trunc_page((vm_offset_t) uap->addr);
764 end = round_page((vm_offset_t) uap->addr + uap->len);
766 if (vm_map_madvise(map, start, end, uap->behav))
771 #ifndef _SYS_SYSPROTO_H_
772 struct mincore_args {
785 struct mincore_args *uap;
787 vm_offset_t addr, first_addr;
788 vm_offset_t end, cend;
793 int vecindex, lastvecindex;
794 vm_map_entry_t current;
795 vm_map_entry_t entry;
797 vm_paddr_t locked_pa;
801 unsigned int timestamp;
805 * Make sure that the addresses presented are valid for user
808 first_addr = addr = trunc_page((vm_offset_t) uap->addr);
809 end = addr + (vm_size_t)round_page(uap->len);
810 map = &td->td_proc->p_vmspace->vm_map;
811 if (end > vm_map_max(map) || end < addr)
815 * Address of byte vector
819 pmap = vmspace_pmap(td->td_proc->p_vmspace);
821 vm_map_lock_read(map);
823 timestamp = map->timestamp;
825 if (!vm_map_lookup_entry(map, addr, &entry)) {
826 vm_map_unlock_read(map);
831 * Do this on a map entry basis so that if the pages are not
832 * in the current processes address space, we can easily look
833 * up the pages elsewhere.
836 for (current = entry;
837 (current != &map->header) && (current->start < end);
838 current = current->next) {
841 * check for contiguity
843 if (current->end < end &&
844 (entry->next == &map->header ||
845 current->next->start > current->end)) {
846 vm_map_unlock_read(map);
851 * ignore submaps (for now) or null objects
853 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
854 current->object.vm_object == NULL)
858 * limit this scan to the current map entry and the
859 * limits for the mincore call
861 if (addr < current->start)
862 addr = current->start;
868 * scan this entry one page at a time
870 while (addr < cend) {
872 * Check pmap first, it is likely faster, also
873 * it can provide info as to whether we are the
874 * one referencing or modifying the page.
880 mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
881 if (locked_pa != 0) {
883 * The page is mapped by this process but not
884 * both accessed and modified. It is also
885 * managed. Acquire the object lock so that
886 * other mappings might be examined.
888 m = PHYS_TO_VM_PAGE(locked_pa);
889 if (m->object != object) {
891 VM_OBJECT_WUNLOCK(object);
893 locked = VM_OBJECT_TRYWLOCK(object);
896 VM_OBJECT_WLOCK(object);
902 KASSERT(m->valid == VM_PAGE_BITS_ALL,
903 ("mincore: page %p is mapped but invalid",
905 } else if (mincoreinfo == 0) {
907 * The page is not mapped by this process. If
908 * the object implements managed pages, then
909 * determine if the page is resident so that
910 * the mappings might be examined.
912 if (current->object.vm_object != object) {
914 VM_OBJECT_WUNLOCK(object);
915 object = current->object.vm_object;
916 VM_OBJECT_WLOCK(object);
918 if (object->type == OBJT_DEFAULT ||
919 object->type == OBJT_SWAP ||
920 object->type == OBJT_VNODE) {
921 pindex = OFF_TO_IDX(current->offset +
922 (addr - current->start));
923 m = vm_page_lookup(object, pindex);
925 vm_page_is_cached(object, pindex))
926 mincoreinfo = MINCORE_INCORE;
927 if (m != NULL && m->valid == 0)
930 mincoreinfo = MINCORE_INCORE;
934 /* Examine other mappings to the page. */
935 if (m->dirty == 0 && pmap_is_modified(m))
938 mincoreinfo |= MINCORE_MODIFIED_OTHER;
940 * The first test for PGA_REFERENCED is an
941 * optimization. The second test is
942 * required because a concurrent pmap
943 * operation could clear the last reference
944 * and set PGA_REFERENCED before the call to
945 * pmap_is_referenced().
947 if ((m->aflags & PGA_REFERENCED) != 0 ||
948 pmap_is_referenced(m) ||
949 (m->aflags & PGA_REFERENCED) != 0)
950 mincoreinfo |= MINCORE_REFERENCED_OTHER;
953 VM_OBJECT_WUNLOCK(object);
956 * subyte may page fault. In case it needs to modify
957 * the map, we release the lock.
959 vm_map_unlock_read(map);
962 * calculate index into user supplied byte vector
964 vecindex = OFF_TO_IDX(addr - first_addr);
967 * If we have skipped map entries, we need to make sure that
968 * the byte vector is zeroed for those skipped entries.
970 while ((lastvecindex + 1) < vecindex) {
971 error = subyte(vec + lastvecindex, 0);
980 * Pass the page information to the user
982 error = subyte(vec + vecindex, mincoreinfo);
989 * If the map has changed, due to the subyte, the previous
990 * output may be invalid.
992 vm_map_lock_read(map);
993 if (timestamp != map->timestamp)
996 lastvecindex = vecindex;
1002 * subyte may page fault. In case it needs to modify
1003 * the map, we release the lock.
1005 vm_map_unlock_read(map);
1008 * Zero the last entries in the byte vector.
1010 vecindex = OFF_TO_IDX(end - first_addr);
1011 while ((lastvecindex + 1) < vecindex) {
1012 error = subyte(vec + lastvecindex, 0);
1021 * If the map has changed, due to the subyte, the previous
1022 * output may be invalid.
1024 vm_map_lock_read(map);
1025 if (timestamp != map->timestamp)
1027 vm_map_unlock_read(map);
1032 #ifndef _SYS_SYSPROTO_H_
1044 struct mlock_args *uap;
1047 return (vm_mlock(td->td_proc, td->td_ucred, uap->addr, uap->len));
1051 vm_mlock(struct proc *proc, struct ucred *cred, const void *addr0, size_t len)
1053 vm_offset_t addr, end, last, start;
1054 vm_size_t npages, size;
1056 unsigned long nsize;
1059 error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
1062 addr = (vm_offset_t)addr0;
1065 start = trunc_page(addr);
1066 end = round_page(last);
1067 if (last < addr || end < addr)
1069 npages = atop(end - start);
1070 if (npages > vm_page_max_wired)
1072 map = &proc->p_vmspace->vm_map;
1074 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1075 if (nsize > lim_cur(proc, RLIMIT_MEMLOCK)) {
1080 if (npages + cnt.v_wire_count > vm_page_max_wired)
1084 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1089 error = vm_map_wire(map, start, end,
1090 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1092 if (error != KERN_SUCCESS) {
1094 racct_set(proc, RACCT_MEMLOCK,
1095 ptoa(pmap_wired_count(map->pmap)));
1099 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct mlockall_args {
1112 sys_mlockall(td, uap)
1114 struct mlockall_args *uap;
1119 map = &td->td_proc->p_vmspace->vm_map;
1120 error = priv_check(td, PRIV_VM_MLOCK);
1124 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1128 * If wiring all pages in the process would cause it to exceed
1129 * a hard resource limit, return ENOMEM.
1131 if (!old_mlock && uap->how & MCL_CURRENT) {
1132 PROC_LOCK(td->td_proc);
1133 if (map->size > lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1134 PROC_UNLOCK(td->td_proc);
1137 PROC_UNLOCK(td->td_proc);
1140 PROC_LOCK(td->td_proc);
1141 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1142 PROC_UNLOCK(td->td_proc);
1147 if (uap->how & MCL_FUTURE) {
1149 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1154 if (uap->how & MCL_CURRENT) {
1156 * P1003.1-2001 mandates that all currently mapped pages
1157 * will be memory resident and locked (wired) upon return
1158 * from mlockall(). vm_map_wire() will wire pages, by
1159 * calling vm_fault_wire() for each page in the region.
1161 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1162 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1163 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
1166 if (error != KERN_SUCCESS) {
1167 PROC_LOCK(td->td_proc);
1168 racct_set(td->td_proc, RACCT_MEMLOCK,
1169 ptoa(pmap_wired_count(map->pmap)));
1170 PROC_UNLOCK(td->td_proc);
1177 #ifndef _SYS_SYSPROTO_H_
1178 struct munlockall_args {
1187 sys_munlockall(td, uap)
1189 struct munlockall_args *uap;
1194 map = &td->td_proc->p_vmspace->vm_map;
1195 error = priv_check(td, PRIV_VM_MUNLOCK);
1199 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1201 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1204 /* Forcibly unwire all pages. */
1205 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1206 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1208 if (error == KERN_SUCCESS) {
1209 PROC_LOCK(td->td_proc);
1210 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1211 PROC_UNLOCK(td->td_proc);
1218 #ifndef _SYS_SYSPROTO_H_
1219 struct munlock_args {
1228 sys_munlock(td, uap)
1230 struct munlock_args *uap;
1232 vm_offset_t addr, end, last, start;
1239 error = priv_check(td, PRIV_VM_MUNLOCK);
1242 addr = (vm_offset_t)uap->addr;
1245 start = trunc_page(addr);
1246 end = round_page(last);
1247 if (last < addr || end < addr)
1249 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1250 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1252 if (error == KERN_SUCCESS) {
1253 PROC_LOCK(td->td_proc);
1254 map = &td->td_proc->p_vmspace->vm_map;
1255 racct_set(td->td_proc, RACCT_MEMLOCK,
1256 ptoa(pmap_wired_count(map->pmap)));
1257 PROC_UNLOCK(td->td_proc);
1260 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1266 * Helper function for vm_mmap. Perform sanity check specific for mmap
1267 * operations on vnodes.
1269 * For VCHR vnodes, the vnode lock is held over the call to
1270 * vm_mmap_cdev() to keep vp->v_rdev valid.
1273 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1274 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1275 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1276 boolean_t *writecounted)
1283 int error, flags, locktype;
1286 cred = td->td_ucred;
1287 if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
1288 locktype = LK_EXCLUSIVE;
1290 locktype = LK_SHARED;
1291 if ((error = vget(vp, locktype, td)) != 0)
1296 if (vp->v_type == VREG) {
1298 * Get the proper underlying object
1304 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1306 vp = (struct vnode *)obj->handle;
1308 * Bypass filesystems obey the mpsafety of the
1309 * underlying fs. Tmpfs never bypasses.
1311 error = vget(vp, locktype, td);
1315 if (locktype == LK_EXCLUSIVE) {
1316 *writecounted = TRUE;
1317 vnode_pager_update_writecount(obj, 0, objsize);
1319 } else if (vp->v_type == VCHR) {
1320 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
1321 vp->v_rdev, foffp, objp);
1329 if ((error = VOP_GETATTR(vp, &va, cred)))
1332 error = mac_vnode_check_mmap(cred, vp, prot, flags);
1336 if ((flags & MAP_SHARED) != 0) {
1337 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1338 if (prot & PROT_WRITE) {
1342 *maxprotp &= ~VM_PROT_WRITE;
1346 * If it is a regular file without any references
1347 * we do not need to sync it.
1348 * Adjust object size to be the size of actual file.
1350 objsize = round_page(va.va_size);
1351 if (va.va_nlink == 0)
1352 flags |= MAP_NOSYNC;
1353 if (obj->type == OBJT_VNODE)
1354 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1357 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
1358 ("wrong object type"));
1359 vm_object_reference(obj);
1369 vfs_mark_atime(vp, cred);
1372 if (error != 0 && *writecounted) {
1373 *writecounted = FALSE;
1374 vnode_pager_update_writecount(obj, objsize, 0);
1385 * Helper function for vm_mmap. Perform sanity check specific for mmap
1386 * operations on cdevs.
1389 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
1390 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1391 struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
1395 int error, flags, ref;
1399 dsw = dev_refthread(cdev, &ref);
1402 if (dsw->d_flags & D_MMAP_ANON) {
1403 dev_relthread(cdev, ref);
1404 *maxprotp = VM_PROT_ALL;
1405 *flagsp |= MAP_ANON;
1409 * cdevs do not provide private mappings of any kind.
1411 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1412 (prot & PROT_WRITE) != 0) {
1413 dev_relthread(cdev, ref);
1416 if (flags & (MAP_PRIVATE|MAP_COPY)) {
1417 dev_relthread(cdev, ref);
1421 * Force device mappings to be shared.
1423 flags |= MAP_SHARED;
1425 error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
1427 dev_relthread(cdev, ref);
1432 * First, try d_mmap_single(). If that is not implemented
1433 * (returns ENODEV), fall back to using the device pager.
1434 * Note that d_mmap_single() must return a reference to the
1435 * object (it needs to bump the reference count of the object
1436 * it returns somehow).
1438 * XXX assumes VM_PROT_* == PROT_*
1440 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1441 dev_relthread(cdev, ref);
1442 if (error != ENODEV)
1444 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1458 * Helper function for vm_mmap. Perform sanity check specific for mmap
1459 * operations on shm file descriptors.
1462 vm_mmap_shm(struct thread *td, vm_size_t objsize,
1463 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1464 struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
1468 if ((*flagsp & MAP_SHARED) != 0 &&
1469 (*maxprotp & VM_PROT_WRITE) == 0 &&
1470 (prot & PROT_WRITE) != 0)
1473 error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
1477 error = shm_mmap(shmfd, objsize, foff, objp);
1488 * Internal version of mmap. Currently used by mmap, exec, and sys5
1489 * shared memory. Handle is either a vnode pointer or NULL for MAP_ANON.
1492 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1493 vm_prot_t maxprot, int flags,
1494 objtype_t handle_type, void *handle,
1498 vm_object_t object = NULL;
1499 struct thread *td = curthread;
1500 int docow, error, findspace, rv;
1501 boolean_t writecounted;
1506 size = round_page(size);
1508 if (map == &td->td_proc->p_vmspace->vm_map) {
1509 PROC_LOCK(td->td_proc);
1510 if (map->size + size > lim_cur(td->td_proc, RLIMIT_VMEM)) {
1511 PROC_UNLOCK(td->td_proc);
1514 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1515 PROC_UNLOCK(td->td_proc);
1518 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1519 if (ptoa(pmap_wired_count(map->pmap)) + size >
1520 lim_cur(td->td_proc, RLIMIT_MEMLOCK)) {
1521 racct_set_force(td->td_proc, RACCT_VMEM,
1523 PROC_UNLOCK(td->td_proc);
1526 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1527 ptoa(pmap_wired_count(map->pmap)) + size);
1529 racct_set_force(td->td_proc, RACCT_VMEM,
1531 PROC_UNLOCK(td->td_proc);
1535 PROC_UNLOCK(td->td_proc);
1539 * We currently can only deal with page aligned file offsets.
1540 * The check is here rather than in the syscall because the
1541 * kernel calls this function internally for other mmaping
1542 * operations (such as in exec) and non-aligned offsets will
1543 * cause pmap inconsistencies...so we want to be sure to
1544 * disallow this in all cases.
1546 if (foff & PAGE_MASK)
1549 if ((flags & MAP_FIXED) == 0) {
1551 *addr = round_page(*addr);
1553 if (*addr != trunc_page(*addr))
1557 writecounted = FALSE;
1560 * Lookup/allocate object.
1562 switch (handle_type) {
1564 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
1565 handle, &foff, &object);
1568 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1569 handle, &foff, &object, &writecounted);
1572 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
1573 handle, foff, &object);
1576 if (handle == NULL) {
1587 if (flags & MAP_ANON) {
1591 * Unnamed anonymous regions always start at 0.
1595 } else if (flags & MAP_PREFAULT_READ)
1596 docow = MAP_PREFAULT;
1598 docow = MAP_PREFAULT_PARTIAL;
1600 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1601 docow |= MAP_COPY_ON_WRITE;
1602 if (flags & MAP_NOSYNC)
1603 docow |= MAP_DISABLE_SYNCER;
1604 if (flags & MAP_NOCORE)
1605 docow |= MAP_DISABLE_COREDUMP;
1606 /* Shared memory is also shared with children. */
1607 if (flags & MAP_SHARED)
1608 docow |= MAP_INHERIT_SHARE;
1610 docow |= MAP_VN_WRITECOUNT;
1612 if (flags & MAP_STACK)
1613 rv = vm_map_stack(map, *addr, size, prot, maxprot,
1614 docow | MAP_STACK_GROWS_DOWN);
1616 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1617 findspace = VMFS_SUPER_SPACE;
1618 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1619 findspace = VMFS_ALIGNED_SPACE(flags >>
1620 MAP_ALIGNMENT_SHIFT);
1622 findspace = VMFS_OPTIMAL_SPACE;
1623 rv = vm_map_find(map, object, foff, addr, size, findspace,
1624 prot, maxprot, docow);
1626 rv = vm_map_fixed(map, object, foff, *addr, size,
1627 prot, maxprot, docow);
1629 if (rv == KERN_SUCCESS) {
1631 * If the process has requested that all future mappings
1632 * be wired, then heed this.
1634 if (map->flags & MAP_WIREFUTURE) {
1635 vm_map_wire(map, *addr, *addr + size,
1636 VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
1637 VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
1641 * If this mapping was accounted for in the vnode's
1642 * writecount, then undo that now.
1645 vnode_pager_release_writecount(object, 0, size);
1647 * Lose the object reference. Will destroy the
1648 * object if it's an unnamed anonymous mapping
1649 * or named anonymous without other references.
1651 vm_object_deallocate(object);
1653 return (vm_mmap_to_errno(rv));
1657 * Translate a Mach VM return code to zero on success or the appropriate errno
1661 vm_mmap_to_errno(int rv)
1667 case KERN_INVALID_ADDRESS:
1670 case KERN_PROTECTION_FAILURE: