2 * SPDX-License-Identifier: BSD-3-Clause
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 * 3. 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
42 * Mapped file (mmap) interface to VM
45 #include <sys/cdefs.h>
46 #include "opt_hwpmc_hooks.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capsicum.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
57 #include <sys/filedesc.h>
60 #include <sys/procctl.h>
61 #include <sys/racct.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/rwlock.h>
65 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
67 #include <sys/fcntl.h>
70 #include <sys/mount.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysent.h>
75 #include <sys/vmmeter.h>
76 #if defined(__amd64__) || defined(__i386__) /* for i386_read_exec */
77 #include <machine/md_var.h>
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
84 #include <vm/vm_param.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vm_pageout.h>
91 #include <vm/vm_extern.h>
92 #include <vm/vm_page.h>
93 #include <vm/vnode_pager.h>
96 #include <sys/pmckern.h>
100 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
101 "Do not apply RLIMIT_MEMLOCK on mlockall");
102 static int mincore_mapped = 1;
103 SYSCTL_INT(_vm, OID_AUTO, mincore_mapped, CTLFLAG_RWTUN, &mincore_mapped, 0,
104 "mincore reports mappings, not residency");
105 static int imply_prot_max = 0;
106 SYSCTL_INT(_vm, OID_AUTO, imply_prot_max, CTLFLAG_RWTUN, &imply_prot_max, 0,
107 "Imply maximum page protections in mmap() when none are specified");
109 _Static_assert(MAXPAGESIZES <= 4, "MINCORE_SUPER too narrow");
111 #ifndef _SYS_SYSPROTO_H_
118 sys_sbrk(struct thread *td, struct sbrk_args *uap)
120 /* Not yet implemented */
124 #ifndef _SYS_SYSPROTO_H_
131 sys_sstk(struct thread *td, struct sstk_args *uap)
133 /* Not yet implemented */
137 #if defined(COMPAT_43)
139 ogetpagesize(struct thread *td, struct ogetpagesize_args *uap)
142 td->td_retval[0] = PAGE_SIZE;
145 #endif /* COMPAT_43 */
148 * Memory Map (mmap) system call. Note that the file offset
149 * and address are allowed to be NOT page aligned, though if
150 * the MAP_FIXED flag it set, both must have the same remainder
151 * modulo the PAGE_SIZE (POSIX 1003.1b). If the address is not
152 * page-aligned, the actual mapping starts at trunc_page(addr)
153 * and the return value is adjusted up by the page offset.
155 * Generally speaking, only character devices which are themselves
156 * memory-based, such as a video framebuffer, can be mmap'd. Otherwise
157 * there would be no cache coherency between a descriptor and a VM mapping
158 * both to the same character device.
160 #ifndef _SYS_SYSPROTO_H_
173 sys_mmap(struct thread *td, struct mmap_args *uap)
176 return (kern_mmap(td, &(struct mmap_req){
177 .mr_hint = (uintptr_t)uap->addr,
179 .mr_prot = uap->prot,
180 .mr_flags = uap->flags,
187 kern_mmap_maxprot(struct proc *p, int prot)
190 if ((p->p_flag2 & P2_PROTMAX_DISABLE) != 0 ||
191 (p->p_fctl0 & NT_FREEBSD_FCTL_PROTMAX_DISABLE) != 0)
193 if (((p->p_flag2 & P2_PROTMAX_ENABLE) != 0 || imply_prot_max) &&
200 kern_mmap(struct thread *td, const struct mmap_req *mrp)
206 vm_offset_t addr, orig_addr;
207 vm_size_t len, pageoff, size;
208 vm_prot_t cap_maxprot;
209 int align, error, fd, flags, max_prot, prot;
211 mmap_check_fp_fn check_fp_fn;
213 orig_addr = addr = mrp->mr_hint;
216 flags = mrp->mr_flags;
219 check_fp_fn = mrp->mr_check_fp_fn;
221 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
223 max_prot = PROT_MAX_EXTRACT(prot);
224 prot = PROT_EXTRACT(prot);
225 if (max_prot != 0 && (max_prot & prot) != prot)
231 * Always honor PROT_MAX if set. If not, default to all
232 * permissions unless we're implying maximum permissions.
235 max_prot = kern_mmap_maxprot(p, prot);
242 * Ignore old flags that used to be defined but did not do anything.
244 flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
247 * Enforce the constraints.
248 * Mapping of length 0 is only allowed for old binaries.
249 * Anonymous mapping shall specify -1 as filedescriptor and
250 * zero position for new code. Be nice to ancient a.out
251 * binaries and correct pos for anonymous mapping, since old
252 * ld.so sometimes issues anonymous map requests with non-zero
255 if (!SV_CURPROC_FLAG(SV_AOUT)) {
256 if ((len == 0 && p->p_osrel >= P_OSREL_MAP_ANON) ||
257 ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
260 if ((flags & MAP_ANON) != 0)
264 if (flags & MAP_STACK) {
266 ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
271 if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
272 MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
273 MAP_PREFAULT_READ | MAP_GUARD | MAP_32BIT | MAP_ALIGNMENT_MASK)) != 0)
275 if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
277 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
279 if (prot != PROT_NONE &&
280 (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
282 if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
283 pos != 0 || (flags & ~(MAP_FIXED | MAP_GUARD | MAP_EXCL |
284 MAP_32BIT | MAP_ALIGNMENT_MASK)) != 0))
288 * Align the file position to a page boundary,
289 * and save its page offset component.
291 pageoff = (pos & PAGE_MASK);
294 /* Compute size from len by rounding (on both ends). */
295 size = len + pageoff; /* low end... */
296 size = round_page(size); /* hi end */
297 /* Check for rounding up to zero. */
301 /* Ensure alignment is at least a page and fits in a pointer. */
302 align = flags & MAP_ALIGNMENT_MASK;
303 if (align != 0 && align != MAP_ALIGNED_SUPER &&
304 (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
305 align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
309 * Check for illegal addresses. Watch out for address wrap... Note
310 * that VM_*_ADDRESS are not constants due to casts (argh).
312 if (flags & MAP_FIXED) {
314 * The specified address must have the same remainder
315 * as the file offset taken modulo PAGE_SIZE, so it
316 * should be aligned after adjustment by pageoff.
319 if (addr & PAGE_MASK)
322 /* Address range must be all in user VM space. */
323 if (!vm_map_range_valid(&vms->vm_map, addr, addr + size))
325 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
327 } else if (flags & MAP_32BIT) {
329 * For MAP_32BIT, override the hint if it is too high and
330 * do not bother moving the mapping past the heap (since
331 * the heap is usually above 2GB).
333 if (addr + size > MAP_32BIT_MAX_ADDR)
337 * XXX for non-fixed mappings where no hint is provided or
338 * the hint would fall in the potential heap space,
339 * place it after the end of the largest possible heap.
341 * For anonymous mappings within the address space of the
342 * calling process, the absence of a hint is handled at a
343 * lower level in order to implement different clustering
344 * strategies for ASLR.
346 if (((flags & MAP_ANON) == 0 && addr == 0) ||
347 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
348 addr < round_page((vm_offset_t)vms->vm_daddr +
349 lim_max(td, RLIMIT_DATA))))
350 addr = round_page((vm_offset_t)vms->vm_daddr +
351 lim_max(td, RLIMIT_DATA));
355 * Return success without mapping anything for old
356 * binaries that request a page-aligned mapping of
357 * length 0. For modern binaries, this function
358 * returns an error earlier.
361 } else if ((flags & MAP_GUARD) != 0) {
362 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
363 VM_PROT_NONE, flags, NULL, pos, FALSE, td);
364 } else if ((flags & MAP_ANON) != 0) {
366 * Mapping blank space is trivial.
368 * This relies on VM_PROT_* matching PROT_*.
370 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
371 max_prot, flags, NULL, pos, FALSE, td);
374 * Mapping file, get fp for validation and don't let the
375 * descriptor disappear on us if we block. Check capability
376 * rights, but also return the maximum rights to be combined
377 * with maxprot later.
379 cap_rights_init_one(&rights, CAP_MMAP);
380 if (prot & PROT_READ)
381 cap_rights_set_one(&rights, CAP_MMAP_R);
382 if ((flags & MAP_SHARED) != 0) {
383 if (prot & PROT_WRITE)
384 cap_rights_set_one(&rights, CAP_MMAP_W);
386 if (prot & PROT_EXEC)
387 cap_rights_set_one(&rights, CAP_MMAP_X);
388 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
391 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
392 p->p_osrel >= P_OSREL_MAP_FSTRICT) {
396 if (check_fp_fn != NULL) {
397 error = check_fp_fn(fp, prot, max_prot & cap_maxprot,
402 if (fp->f_ops == &shm_ops && shm_largepage(fp->f_data))
404 /* This relies on VM_PROT_* matching PROT_*. */
405 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
406 max_prot & cap_maxprot, flags, pos, td);
410 td->td_retval[0] = addr + pageoff;
418 #if defined(COMPAT_FREEBSD6)
420 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
422 return (kern_mmap(td, &(struct mmap_req){
423 .mr_hint = (uintptr_t)uap->addr,
425 .mr_prot = uap->prot,
426 .mr_flags = uap->flags,
434 #ifndef _SYS_SYSPROTO_H_
445 ommap(struct thread *td, struct ommap_args *uap)
447 return (kern_ommap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
448 uap->flags, uap->fd, uap->pos));
452 kern_ommap(struct thread *td, uintptr_t hint, int len, int oprot,
453 int oflags, int fd, long pos)
455 static const char cvtbsdprot[8] = {
459 PROT_EXEC | PROT_WRITE,
461 PROT_EXEC | PROT_READ,
462 PROT_WRITE | PROT_READ,
463 PROT_EXEC | PROT_WRITE | PROT_READ,
470 #define OMAP_ANON 0x0002
471 #define OMAP_COPY 0x0020
472 #define OMAP_SHARED 0x0010
473 #define OMAP_FIXED 0x0100
475 prot = cvtbsdprot[oprot & 0x7];
476 #if (defined(COMPAT_FREEBSD32) && defined(__amd64__)) || defined(__i386__)
477 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
482 if (oflags & OMAP_ANON)
484 if (oflags & OMAP_COPY)
486 if (oflags & OMAP_SHARED)
489 flags |= MAP_PRIVATE;
490 if (oflags & OMAP_FIXED)
492 return (kern_mmap(td, &(struct mmap_req){
501 #endif /* COMPAT_43 */
503 #ifndef _SYS_SYSPROTO_H_
511 sys_msync(struct thread *td, struct msync_args *uap)
514 return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
518 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
526 pageoff = (addr & PAGE_MASK);
529 size = (vm_size_t) round_page(size);
530 if (addr + size < addr)
533 if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
536 map = &td->td_proc->p_vmspace->vm_map;
539 * Clean the pages and interpret the return value.
541 rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
542 (flags & MS_INVALIDATE) != 0);
546 case KERN_INVALID_ADDRESS:
548 case KERN_INVALID_ARGUMENT:
557 #ifndef _SYS_SYSPROTO_H_
564 sys_munmap(struct thread *td, struct munmap_args *uap)
567 return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
571 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
574 struct pmckern_map_out pkm;
575 vm_map_entry_t entry;
578 vm_offset_t addr, end;
587 pageoff = (addr & PAGE_MASK);
590 size = (vm_size_t) round_page(size);
592 map = &td->td_proc->p_vmspace->vm_map;
593 if (!vm_map_range_valid(map, addr, end))
599 if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
602 * Inform hwpmc if the address range being unmapped contains
603 * an executable region.
605 pkm.pm_address = (uintptr_t) NULL;
606 if (vm_map_lookup_entry(map, addr, &entry)) {
607 for (; entry->start < end;
608 entry = vm_map_entry_succ(entry)) {
609 if (vm_map_check_protection(map, entry->start,
610 entry->end, VM_PROT_EXECUTE) == TRUE) {
611 pkm.pm_address = (uintptr_t) addr;
612 pkm.pm_size = (size_t) size;
619 rv = vm_map_delete(map, addr, end);
622 if (rv == KERN_SUCCESS && __predict_false(pmc_handled)) {
623 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
624 vm_map_lock_downgrade(map);
625 if (pkm.pm_address != (uintptr_t) NULL)
626 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
627 vm_map_unlock_read(map);
632 return (vm_mmap_to_errno(rv));
635 #ifndef _SYS_SYSPROTO_H_
636 struct mprotect_args {
643 sys_mprotect(struct thread *td, struct mprotect_args *uap)
646 return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len,
651 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot,
656 int vm_error, max_prot;
659 if ((prot & ~(_PROT_ALL | PROT_MAX(_PROT_ALL))) != 0)
661 max_prot = PROT_MAX_EXTRACT(prot);
662 prot = PROT_EXTRACT(prot);
663 pageoff = (addr & PAGE_MASK);
666 size = (vm_size_t) round_page(size);
667 #ifdef COMPAT_FREEBSD32
668 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
669 if (((addr + size) & 0xffffffff) < addr)
673 if (addr + size < addr)
676 flags |= VM_MAP_PROTECT_SET_PROT;
678 flags |= VM_MAP_PROTECT_SET_MAXPROT;
679 vm_error = vm_map_protect(&td->td_proc->p_vmspace->vm_map,
680 addr, addr + size, prot, max_prot, flags);
685 case KERN_PROTECTION_FAILURE:
687 case KERN_RESOURCE_SHORTAGE:
689 case KERN_OUT_OF_BOUNDS:
695 #ifndef _SYS_SYSPROTO_H_
696 struct minherit_args {
703 sys_minherit(struct thread *td, struct minherit_args *uap)
706 return (kern_minherit(td, (uintptr_t)uap->addr, uap->len,
711 kern_minherit(struct thread *td, uintptr_t addr0, size_t len, int inherit0)
714 vm_size_t size, pageoff;
715 vm_inherit_t inherit;
717 addr = (vm_offset_t)addr0;
721 pageoff = (addr & PAGE_MASK);
724 size = (vm_size_t) round_page(size);
725 if (addr + size < addr)
728 switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
729 addr + size, inherit)) {
732 case KERN_PROTECTION_FAILURE:
738 #ifndef _SYS_SYSPROTO_H_
739 struct madvise_args {
747 sys_madvise(struct thread *td, struct madvise_args *uap)
750 return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
754 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
757 vm_offset_t addr, end, start;
761 * Check for our special case, advising the swap pager we are
764 if (behav == MADV_PROTECT) {
766 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
767 PROC_SPROTECT, &flags));
771 * Check for illegal addresses. Watch out for address wrap... Note
772 * that VM_*_ADDRESS are not constants due to casts (argh).
774 map = &td->td_proc->p_vmspace->vm_map;
776 if (!vm_map_range_valid(map, addr, addr + len))
780 * Since this routine is only advisory, we default to conservative
783 start = trunc_page(addr);
784 end = round_page(addr + len);
787 * vm_map_madvise() checks for illegal values of behav.
789 return (vm_map_madvise(map, start, end, behav));
792 #ifndef _SYS_SYSPROTO_H_
793 struct mincore_args {
801 sys_mincore(struct thread *td, struct mincore_args *uap)
804 return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
808 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
812 vm_map_entry_t current, entry;
814 vm_offset_t addr, cend, end, first_addr;
818 int error, lastvecindex, mincoreinfo, vecindex;
819 unsigned int timestamp;
822 * Make sure that the addresses presented are valid for user
825 first_addr = addr = trunc_page(addr0);
826 end = round_page(addr0 + len);
827 map = &td->td_proc->p_vmspace->vm_map;
828 if (end > vm_map_max(map) || end < addr)
831 pmap = vmspace_pmap(td->td_proc->p_vmspace);
833 vm_map_lock_read(map);
835 timestamp = map->timestamp;
837 if (!vm_map_lookup_entry(map, addr, &entry)) {
838 vm_map_unlock_read(map);
843 * Do this on a map entry basis so that if the pages are not
844 * in the current processes address space, we can easily look
845 * up the pages elsewhere.
848 while (entry->start < end) {
850 * check for contiguity
853 entry = vm_map_entry_succ(current);
854 if (current->end < end &&
855 entry->start > current->end) {
856 vm_map_unlock_read(map);
861 * ignore submaps (for now) or null objects
863 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
864 current->object.vm_object == NULL)
868 * limit this scan to the current map entry and the
869 * limits for the mincore call
871 if (addr < current->start)
872 addr = current->start;
877 for (; addr < cend; addr += PAGE_SIZE) {
879 * Check pmap first, it is likely faster, also
880 * it can provide info as to whether we are the
881 * one referencing or modifying the page.
887 mincoreinfo = pmap_mincore(pmap, addr, &pa);
888 if (mincore_mapped) {
890 * We only care about this pmap's
891 * mapping of the page, if any.
894 } else if (pa != 0) {
896 * The page is mapped by this process but not
897 * both accessed and modified. It is also
898 * managed. Acquire the object lock so that
899 * other mappings might be examined. The page's
900 * identity may change at any point before its
901 * object lock is acquired, so re-validate if
904 m = PHYS_TO_VM_PAGE(pa);
905 while (object == NULL || m->object != object) {
907 VM_OBJECT_WUNLOCK(object);
908 object = atomic_load_ptr(&m->object);
911 VM_OBJECT_WLOCK(object);
913 if (pa != pmap_extract(pmap, addr))
915 KASSERT(vm_page_all_valid(m),
916 ("mincore: page %p is mapped but invalid",
918 } else if (mincoreinfo == 0) {
920 * The page is not mapped by this process. If
921 * the object implements managed pages, then
922 * determine if the page is resident so that
923 * the mappings might be examined.
925 if (current->object.vm_object != object) {
927 VM_OBJECT_WUNLOCK(object);
928 object = current->object.vm_object;
929 VM_OBJECT_WLOCK(object);
931 if ((object->flags & OBJ_SWAP) != 0 ||
932 object->type == OBJT_VNODE) {
933 pindex = OFF_TO_IDX(current->offset +
934 (addr - current->start));
935 m = vm_page_lookup(object, pindex);
936 if (m != NULL && vm_page_none_valid(m))
939 mincoreinfo = MINCORE_INCORE;
943 VM_OBJECT_ASSERT_WLOCKED(m->object);
945 /* Examine other mappings of the page. */
946 if (m->dirty == 0 && pmap_is_modified(m))
949 mincoreinfo |= MINCORE_MODIFIED_OTHER;
952 * The first test for PGA_REFERENCED is an
953 * optimization. The second test is
954 * required because a concurrent pmap
955 * operation could clear the last reference
956 * and set PGA_REFERENCED before the call to
957 * pmap_is_referenced().
959 if ((m->a.flags & PGA_REFERENCED) != 0 ||
960 pmap_is_referenced(m) ||
961 (m->a.flags & PGA_REFERENCED) != 0)
962 mincoreinfo |= MINCORE_REFERENCED_OTHER;
965 VM_OBJECT_WUNLOCK(object);
968 * subyte may page fault. In case it needs to modify
969 * the map, we release the lock.
971 vm_map_unlock_read(map);
974 * calculate index into user supplied byte vector
976 vecindex = atop(addr - first_addr);
979 * If we have skipped map entries, we need to make sure that
980 * the byte vector is zeroed for those skipped entries.
982 while ((lastvecindex + 1) < vecindex) {
984 error = subyte(vec + lastvecindex, 0);
992 * Pass the page information to the user
994 error = subyte(vec + vecindex, mincoreinfo);
1001 * If the map has changed, due to the subyte, the previous
1002 * output may be invalid.
1004 vm_map_lock_read(map);
1005 if (timestamp != map->timestamp)
1008 lastvecindex = vecindex;
1013 * subyte may page fault. In case it needs to modify
1014 * the map, we release the lock.
1016 vm_map_unlock_read(map);
1019 * Zero the last entries in the byte vector.
1021 vecindex = atop(end - first_addr);
1022 while ((lastvecindex + 1) < vecindex) {
1024 error = subyte(vec + lastvecindex, 0);
1032 * If the map has changed, due to the subyte, the previous
1033 * output may be invalid.
1035 vm_map_lock_read(map);
1036 if (timestamp != map->timestamp)
1038 vm_map_unlock_read(map);
1043 #ifndef _SYS_SYSPROTO_H_
1050 sys_mlock(struct thread *td, struct mlock_args *uap)
1053 return (kern_mlock(td->td_proc, td->td_ucred,
1054 __DECONST(uintptr_t, uap->addr), uap->len));
1058 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
1060 vm_offset_t addr, end, last, start;
1061 vm_size_t npages, size;
1063 unsigned long nsize;
1066 error = priv_check_cred(cred, PRIV_VM_MLOCK);
1072 start = trunc_page(addr);
1073 end = round_page(last);
1074 if (last < addr || end < addr)
1076 npages = atop(end - start);
1077 if (npages > vm_page_max_user_wired)
1079 map = &proc->p_vmspace->vm_map;
1081 nsize = ptoa(npages + pmap_wired_count(map->pmap));
1082 if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
1090 error = racct_set(proc, RACCT_MEMLOCK, nsize);
1096 error = vm_map_wire(map, start, end,
1097 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1099 if (racct_enable && error != KERN_SUCCESS) {
1101 racct_set(proc, RACCT_MEMLOCK,
1102 ptoa(pmap_wired_count(map->pmap)));
1109 case KERN_INVALID_ARGUMENT:
1116 #ifndef _SYS_SYSPROTO_H_
1117 struct mlockall_args {
1123 sys_mlockall(struct thread *td, struct mlockall_args *uap)
1128 map = &td->td_proc->p_vmspace->vm_map;
1129 error = priv_check(td, PRIV_VM_MLOCK);
1133 if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
1137 * If wiring all pages in the process would cause it to exceed
1138 * a hard resource limit, return ENOMEM.
1140 if (!old_mlock && uap->how & MCL_CURRENT) {
1141 if (map->size > lim_cur(td, RLIMIT_MEMLOCK))
1146 PROC_LOCK(td->td_proc);
1147 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
1148 PROC_UNLOCK(td->td_proc);
1154 if (uap->how & MCL_FUTURE) {
1156 vm_map_modflags(map, MAP_WIREFUTURE, 0);
1161 if (uap->how & MCL_CURRENT) {
1163 * P1003.1-2001 mandates that all currently mapped pages
1164 * will be memory resident and locked (wired) upon return
1165 * from mlockall(). vm_map_wire() will wire pages, by
1166 * calling vm_fault_wire() for each page in the region.
1168 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
1169 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1170 if (error == KERN_SUCCESS)
1172 else if (error == KERN_RESOURCE_SHORTAGE)
1178 if (racct_enable && error != KERN_SUCCESS) {
1179 PROC_LOCK(td->td_proc);
1180 racct_set(td->td_proc, RACCT_MEMLOCK,
1181 ptoa(pmap_wired_count(map->pmap)));
1182 PROC_UNLOCK(td->td_proc);
1189 #ifndef _SYS_SYSPROTO_H_
1190 struct munlockall_args {
1196 sys_munlockall(struct thread *td, struct munlockall_args *uap)
1201 map = &td->td_proc->p_vmspace->vm_map;
1202 error = priv_check(td, PRIV_VM_MUNLOCK);
1206 /* Clear the MAP_WIREFUTURE flag from this vm_map. */
1208 vm_map_modflags(map, 0, MAP_WIREFUTURE);
1211 /* Forcibly unwire all pages. */
1212 error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
1213 VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
1215 if (racct_enable && error == KERN_SUCCESS) {
1216 PROC_LOCK(td->td_proc);
1217 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
1218 PROC_UNLOCK(td->td_proc);
1225 #ifndef _SYS_SYSPROTO_H_
1226 struct munlock_args {
1232 sys_munlock(struct thread *td, struct munlock_args *uap)
1235 return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
1239 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
1241 vm_offset_t addr, end, last, start;
1247 error = priv_check(td, PRIV_VM_MUNLOCK);
1252 start = trunc_page(addr);
1253 end = round_page(last);
1254 if (last < addr || end < addr)
1256 error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
1257 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
1259 if (racct_enable && error == KERN_SUCCESS) {
1260 PROC_LOCK(td->td_proc);
1261 map = &td->td_proc->p_vmspace->vm_map;
1262 racct_set(td->td_proc, RACCT_MEMLOCK,
1263 ptoa(pmap_wired_count(map->pmap)));
1264 PROC_UNLOCK(td->td_proc);
1267 return (error == KERN_SUCCESS ? 0 : ENOMEM);
1273 * Helper function for vm_mmap. Perform sanity check specific for mmap
1274 * operations on vnodes.
1277 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
1278 vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
1279 struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
1280 boolean_t *writecounted)
1289 cred = td->td_ucred;
1290 writex = (*maxprotp & VM_PROT_WRITE) != 0 &&
1291 (*flagsp & MAP_SHARED) != 0;
1292 if ((error = vget(vp, LK_SHARED)) != 0)
1294 AUDIT_ARG_VNODE1(vp);
1298 if (vp->v_type == VREG) {
1300 * Get the proper underlying object
1306 if (obj->type == OBJT_VNODE && obj->handle != vp) {
1308 vp = (struct vnode *)obj->handle;
1310 * Bypass filesystems obey the mpsafety of the
1311 * underlying fs. Tmpfs never bypasses.
1313 error = vget(vp, LK_SHARED);
1318 *writecounted = TRUE;
1319 vm_pager_update_writecount(obj, 0, objsize);
1325 if ((error = VOP_GETATTR(vp, &va, cred)))
1328 /* This relies on VM_PROT_* matching PROT_*. */
1329 error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
1333 if ((flags & MAP_SHARED) != 0) {
1334 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
1335 if (prot & VM_PROT_WRITE) {
1339 *maxprotp &= ~VM_PROT_WRITE;
1343 * If it is a regular file without any references
1344 * we do not need to sync it.
1345 * Adjust object size to be the size of actual file.
1347 objsize = round_page(va.va_size);
1348 if (va.va_nlink == 0)
1349 flags |= MAP_NOSYNC;
1350 if (obj->type == OBJT_VNODE) {
1351 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
1358 KASSERT((obj->flags & OBJ_SWAP) != 0, ("wrong object type"));
1359 vm_object_reference(obj);
1360 #if VM_NRESERVLEVEL > 0
1361 if ((obj->flags & OBJ_COLORED) == 0) {
1362 VM_OBJECT_WLOCK(obj);
1363 vm_object_color(obj, 0);
1364 VM_OBJECT_WUNLOCK(obj);
1374 if (error != 0 && *writecounted) {
1375 *writecounted = FALSE;
1376 vm_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, vm_prot_t prot,
1390 vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
1391 vm_ooffset_t *foff, vm_object_t *objp)
1398 if (dsw->d_flags & D_MMAP_ANON) {
1401 *maxprotp = VM_PROT_ALL;
1402 *flagsp |= MAP_ANON;
1406 * cdevs do not provide private mappings of any kind.
1408 if ((*maxprotp & VM_PROT_WRITE) == 0 &&
1409 (prot & VM_PROT_WRITE) != 0)
1411 if (flags & (MAP_PRIVATE|MAP_COPY))
1414 * Force device mappings to be shared.
1416 flags |= MAP_SHARED;
1418 error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
1423 * First, try d_mmap_single(). If that is not implemented
1424 * (returns ENODEV), fall back to using the device pager.
1425 * Note that d_mmap_single() must return a reference to the
1426 * object (it needs to bump the reference count of the object
1427 * it returns somehow).
1429 * XXX assumes VM_PROT_* == PROT_*
1431 error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
1432 if (error != ENODEV)
1434 obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
1444 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1445 vm_prot_t maxprot, int flags,
1446 objtype_t handle_type, void *handle,
1450 struct thread *td = curthread;
1452 boolean_t writecounted;
1457 size = round_page(size);
1459 writecounted = FALSE;
1461 switch (handle_type) {
1468 dsw = dev_refthread(cdev, &ref);
1471 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
1472 dsw, &foff, &object);
1473 dev_relthread(cdev, ref);
1477 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
1478 handle, &foff, &object, &writecounted);
1487 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1488 foff, writecounted, td);
1489 if (error != 0 && object != NULL) {
1491 * If this mapping was accounted for in the vnode's
1492 * writecount, then undo that now.
1495 vm_pager_release_writecount(object, 0, size);
1496 vm_object_deallocate(object);
1502 kern_mmap_racct_check(struct thread *td, vm_map_t map, vm_size_t size)
1506 RACCT_PROC_LOCK(td->td_proc);
1507 if (map->size + size > lim_cur(td, RLIMIT_VMEM)) {
1508 RACCT_PROC_UNLOCK(td->td_proc);
1511 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
1512 RACCT_PROC_UNLOCK(td->td_proc);
1515 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
1516 if (ptoa(pmap_wired_count(map->pmap)) + size >
1517 lim_cur(td, RLIMIT_MEMLOCK)) {
1518 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
1519 RACCT_PROC_UNLOCK(td->td_proc);
1522 error = racct_set(td->td_proc, RACCT_MEMLOCK,
1523 ptoa(pmap_wired_count(map->pmap)) + size);
1525 racct_set_force(td->td_proc, RACCT_VMEM, map->size);
1526 RACCT_PROC_UNLOCK(td->td_proc);
1530 RACCT_PROC_UNLOCK(td->td_proc);
1535 * Internal version of mmap that maps a specific VM object into an
1536 * map. Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
1539 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
1540 vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
1541 boolean_t writecounted, struct thread *td)
1543 vm_offset_t default_addr, max_addr;
1544 int docow, error, findspace, rv;
1547 curmap = map == &td->td_proc->p_vmspace->vm_map;
1549 error = kern_mmap_racct_check(td, map, size);
1555 * We currently can only deal with page aligned file offsets.
1556 * The mmap() system call already enforces this by subtracting
1557 * the page offset from the file offset, but checking here
1558 * catches errors in device drivers (e.g. d_single_mmap()
1559 * callbacks) and other internal mapping requests (such as in
1562 if (foff & PAGE_MASK)
1565 if ((flags & MAP_FIXED) == 0) {
1567 *addr = round_page(*addr);
1569 if (*addr != trunc_page(*addr))
1574 if (flags & MAP_ANON) {
1575 if (object != NULL || foff != 0)
1578 } else if (flags & MAP_PREFAULT_READ)
1579 docow = MAP_PREFAULT;
1581 docow = MAP_PREFAULT_PARTIAL;
1583 if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
1584 docow |= MAP_COPY_ON_WRITE;
1585 if (flags & MAP_NOSYNC)
1586 docow |= MAP_DISABLE_SYNCER;
1587 if (flags & MAP_NOCORE)
1588 docow |= MAP_DISABLE_COREDUMP;
1589 /* Shared memory is also shared with children. */
1590 if (flags & MAP_SHARED)
1591 docow |= MAP_INHERIT_SHARE;
1593 docow |= MAP_WRITECOUNT;
1594 if (flags & MAP_STACK) {
1597 docow |= MAP_STACK_GROWS_DOWN;
1599 if ((flags & MAP_EXCL) != 0)
1600 docow |= MAP_CHECK_EXCL;
1601 if ((flags & MAP_GUARD) != 0)
1602 docow |= MAP_CREATE_GUARD;
1605 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
1606 findspace = VMFS_SUPER_SPACE;
1607 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
1608 findspace = VMFS_ALIGNED_SPACE(flags >>
1609 MAP_ALIGNMENT_SHIFT);
1611 findspace = VMFS_OPTIMAL_SPACE;
1613 if ((flags & MAP_32BIT) != 0)
1614 max_addr = MAP_32BIT_MAX_ADDR;
1617 round_page((vm_offset_t)td->td_proc->p_vmspace->
1618 vm_daddr + lim_max(td, RLIMIT_DATA));
1619 if ((flags & MAP_32BIT) != 0)
1621 rv = vm_map_find_min(map, object, foff, addr, size,
1622 default_addr, max_addr, findspace, prot, maxprot,
1625 rv = vm_map_find(map, object, foff, addr, size,
1626 max_addr, findspace, prot, maxprot, docow);
1629 rv = vm_map_fixed(map, object, foff, *addr, size,
1630 prot, maxprot, docow);
1633 if (rv == KERN_SUCCESS) {
1635 * If the process has requested that all future mappings
1636 * be wired, then heed this.
1638 if ((map->flags & MAP_WIREFUTURE) != 0) {
1640 if ((map->flags & MAP_WIREFUTURE) != 0)
1641 (void)vm_map_wire_locked(map, *addr,
1642 *addr + size, VM_MAP_WIRE_USER |
1643 ((flags & MAP_STACK) ? VM_MAP_WIRE_HOLESOK :
1644 VM_MAP_WIRE_NOHOLES));
1648 return (vm_mmap_to_errno(rv));
1652 * Translate a Mach VM return code to zero on success or the appropriate errno
1656 vm_mmap_to_errno(int rv)
1662 case KERN_INVALID_ADDRESS:
1665 case KERN_PROTECTION_FAILURE: